问题
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.
In which format does Android provide the byte[] data in it's callback and how to convert it to an OpenCV Mat in the color-format BGR?
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));
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);
......
回答1:
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);
来源:https://stackoverflow.com/questions/29129906/how-to-retrieve-a-new-picture-taken-by-the-camera-as-opencv-mat-on-android