问题描述
我正在尝试使用Android设备拍照.图片必须转换为Mat才能作为输入,我希望在API中提供其计算结果.
I am trying to take a picture with an Android device. The picture must be converted as Mat to be an input for a computation of which I like to provide the results within an API.
Android在回调中以哪种格式提供byte []数据,以及如何将其转换为彩色格式BGR的OpenCV Mat?
第一个问题:如何在没有SurfaceView的情况下拍摄照片".我使用了SurfaceTexture,它一定不可见.
The first problem: "How to take the picture without a SurfaceView" is solved. I used a SurfaceTexture, which must not be visible.
mCamera = Camera.open();
mCamera.setPreviewTexture(new SurfaceTexture(10));
因此,我能够开始预览并拍照.但是byte []数据是哪种格式,以及如何将其转换为OpenCV BGR Mat?
So I was able to start the preview and take a picture. But in which format is the byte[] data and how to convert it to an OpenCV BGR Mat?
mCamera.startPreview();
mCamera.takePicture(null, null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
Log.e(MainActivity.APP_ID, "picture-taken");
android.hardware.Camera.Size pictureSize = camera.getParameters().getPictureSize();
Mat mat = new Mat(new Size(pictureSize.width, pictureSize.height), CvType.CV_8U);
mat.put(0,0,data);
mat.reshape(0, pictureSize.height);
// Imgproc.cvtColor(mat, mat, Imgproc.COLOR_YUV420sp2RGBA);
......
推荐答案
正如tokan在问题中的评论所指出的那样,此解决方案非常有效:
As tokan pointed on his comment in the question, this solution works great:
android.hardware.Camera.Size pictureSize = camera.getParameters().getPictureSize();
Mat mat = new Mat(new Size(pictureSize.width, pictureSize.height), CvType.CV_8U);
mat.put(0, 0, data);
Mat img = Imgcodecs.imdecode(mat, Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);
这篇关于如何在Android上以OpenCV Mat的形式检索相机拍摄的新照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!