Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onPreviewFrame returning wrong byte[] length

Tags:

android

I've trying to get byte-array and copy from there to another array some rows of picture previewing whith onPreviewFrame(). So I prepare Camera and new byte-array

        try {
        if (cam != null) {

            Camera.Parameters parameters = cam.getParameters();
            parameters.setPreviewFrameRate(25);
            parameters.setPictureFormat(ImageFormat.JPEG);
            cam.setParameters(parameters);
            cam.setPreviewDisplay(holder);
            cam.startPreview();

            arr = new byte[(parameters.getPreviewSize().height)
                    * (parameters.getPreviewSize().width)];

            Log.i("" + parameters.getPictureSize().height, "   "
                    + parameters.getPictureSize().width);


        }
    } catch (IOException e) {
        Log.d("CAMERA", e.getMessage());
    }

And Log tell's me :

480 640

So arr.length = 307200

But:

public void onPreviewFrame(byte[] data, Camera camera) {    
    Log.i(arr.length+" ",data.length+"");
}

arr.length = 307200 (which of course means Frame Size = 640*480)

but data.length = 460800 (and it means Frame Size = 800*576)

And when I try to copy Data to Arr with System.arraycopy(data, 0, arr, 0, arr.length); and then export picture:

FileOutputStream outStream = null;
    try {
        outStream = new FileOutputStream("/sdcard/new.jpg");

        Camera.Parameters parameters = cam.getParameters();
        Size size = parameters.getPreviewSize();

        YuvImage image = new YuvImage(arr, parameters.getPreviewFormat(),
                size.width, size.height, null);
        image.compressToJpeg(
                new Rect(0, 0, image.getWidth(), image.getHeight()), 90,
                outStream);

        outStream.flush();
        outStream.close();
    } catch (FileNotFoundException e) {
        Log.d("CAMERA", e.getMessage());
    } catch (IOException e) {
        Log.d("CAMERA", e.getMessage());
    }

Picture become green:

enter image description here

So what I need to do to make data-array become 640*480? Or maybe you know another way to get single row from (for example) Bitmap and add it to another Bitmap?

like image 598
Vitaliy Avatar asked Oct 20 '12 19:10

Vitaliy


1 Answers

The correct array size is new byte[previewSize.height * previewSize.width * (ImageFormat.getBitsPerPixel(parameters.getPreviewFormat())) / 8]; (different formats have different bits-per-pixel values, get them with ImageFormat.getBitsPerPixel)

like image 140
molnarm Avatar answered Oct 16 '22 22:10

molnarm