Jeffrey Chen
Jeffrey Chen

Reputation: 1967

How to use CameraSource to detect custom visual code which need color information

I want to use CameraSource to detect some visual code (which is not any kind of Barcode). I implements Detector and its detect(Frame frame) method. However, when I call frame.getBitmap() in the detect method, it always returns null. I know Frame has another method, getGrayscaleImageData(), but detecting the code needs color information. It seems that CameraSource only pass the gray-scale image data to its underlying detector.

So, is there a way to detect this code by CameraSource? Or should I abandon CameraSource and find another way?

Upvotes: 0

Views: 284

Answers (2)

Ayad Kara Kâhya
Ayad Kara Kâhya

Reputation: 118

Found it :D this code return colored bitmap so fast but if it's the Front camera you may have to flip/rotate according to device.

    public SparseArray detect(Frame frame) {
    byte[] bytes = frame.getGrayscaleImageData().array();
    YuvImage yuvImage = new YuvImage(frame.getGrayscaleImageData().array(), ImageFormat.NV21, frame.getMetadata().getWidth(), frame.getMetadata().getHeight(), null);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    yuvImage.compressToJpeg(new Rect(0, 0, frame.getMetadata().getWidth(), frame.getMetadata().getHeight()), 100, byteArrayOutputStream);
    byte[] jpegArray = byteArrayOutputStream.toByteArray();
    Bitmap bitmap = BitmapFactory.decodeByteArray(jpegArray, 0, jpegArray.length);//this bitmap is colored.
    return null; 
}

Upvotes: 0

pm0733464
pm0733464

Reputation: 2862

In the current release, CameraSource actually does return the full color information for the image from getGrayscaleImageData. The leading bytes of what is returned is the grayscale layer of the image (the Y channel), but the bytes beyond that have the color information. The format details depend upon what image format you specified in setting up the CameraSource (the default is NV21 format).

Upvotes: 1

Related Questions