Ask Your Question
0

Android openCV byte[] to mat to byte[]

asked 2016-04-27 10:06:35 -0600

Dimebag gravatar image

My goal is to add an overlay on the camera preview that will find book edges. For that, I override the onPreviewFrame where I do the following:

public void onPreviewFrame(byte[] data, Camera camera) {
    Camera.Parameters parameters = camera.getParameters();
    int width = parameters.getPreviewSize().width;
    int height = parameters.getPreviewSize().height;
    Mat mat = new Mat((int) (height*1.5), width, CvType.CV_8UC1);
    mat.put(0,0,data);
    byte[] bytes = new byte[(int) (height*width*1.5)];
    mat.get(0,0,bytes);

    if (!test) {        //to only do once
        File pictureFile = getOutputMediaFile();
        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(bytes);
            fos.close();
            Uri picUri = Uri.fromFile(pictureFile);
            updateGallery(picUri);
            test = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

For now I simply want to take one of the previews and save it after the conversion to mat.

After spending countless hours getting the above to look right, the saved picture cannot be seen on my testing phone (LG Leon). I can't seem to find the issue. Am I mixing the height/width because I'm taking pictures in portrait mode? I tried switching them and still doesn't work. Where is the problem?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-04-29 11:40:31 -0600

Dimebag gravatar image

Seems quite inefficient but it works for me (for now):

//get the camera parameters
Camera.Parameters parameters = camera.getParameters();
int width = parameters.getPreviewSize().width;
int height = parameters.getPreviewSize().height;

//convert the byte[] to Bitmap through YuvImage; 
//make sure the previewFormat is NV21 (I set it so somewhere before)
YuvImage yuv = new YuvImage(data, parameters.getPreviewFormat(), width, height, null);
ByteArrayOutputStream out = new ByteArrayOutputStream();
yuv.compressToJpeg(new Rect(0, 0, width, height), 70, out);
Bitmap bmp = BitmapFactory.decodeByteArray(out.toByteArray(), 0, out.size());

//convert Bitmap to Mat; note the bitmap config ARGB_8888 conversion that 
//allows you to use other image processing methods and still save at the end
Mat orig = new Mat();
bmp = bmp.copy(Bitmap.Config.ARGB_8888, true);
Utils.bitmapToMat(bmp, orig);

//here you do whatever you want with the Mat

//Mat to Bitmap to OutputStream to byte[] to File
Utils.matToBitmap(orig, bmp);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 70, stream);
byte[] bytes = stream.toByteArray();
File pictureFile = getOutputMediaFile();
try {
    FileOutputStream fos = new FileOutputStream(pictureFile);
    fos.write(bytes);
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-27 10:06:35 -0600

Seen: 4,920 times

Last updated: Apr 29 '16