Brian
Brian

Reputation: 313

Android Camera Preview - take only a part of the screen data

I want to take only a part of the the screen data from a preview video callback to reduce the time of the process. The probleme is I only know how to take the whole screen with OnPreviewFrame:

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
        myData = data;
        // +get camera resolution x, y
}

And then with this data get the image :

private Bitmap getBitmapFromYUV(byte[] data, int width, int height)
{
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    YuvImage yuvImage = new YuvImage(data, ImageFormat.NV21, width, height, null);
    yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, out);

    byte[] imageBytes = out.toByteArray();
    Bitmap image = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);

    return image;
}

And then I take the part of the image taken I want :

cutImage = Bitmap.createBitmap(image, xOffset, yOffset, customWidth, customHeight);

The problem is that I need to take lots of images to apply some image processing on it and that's why I want to reduce the time it takes to get the images. Instead of taking the whole screen and then crop it, I want to immediatly get the cropped image. Is there a way to get the part of the screen data ?

Upvotes: 2

Views: 668

Answers (1)

Brian
Brian

Reputation: 313

Ok I finally found something, I still record all the data of the camera but when using compressToJpeg I crop the picture with a custom Rect. Maybe there is something better to do before this but this is still a good improvement. Here are my changes :

yuvImage.compressToJpeg(new Rect(offsetX, offsetY, sizeCaptureX + offsetX, sizeCaptureY + offsetY ), 100, out);

Upvotes: 1

Related Questions