Android image data passed to some pits in C + +

Source: Internet
Author: User

Recently in an Android image recognition app, you get image data through a camera preview or a photo shoot function, and then pass the image data to the local C + + image recognition so library. The most time spent in this process is the data transfer problem. With this pit, I warn the future!
First, there are a few things to note:
1. The callback function of the Android Camera preview mode is the byte [] data parameter, which is YUV data 2. Android camera Snapshot mode callback function of the byte [] data parameter, which is ARGB8888
3. Java Data byte[] is inconsistent with the unsigned char data range in C + +, Java byte: -128~127, C + + unsigned char:0~255.
My application needs are: 1. Capture Mode calls C + + recognition Library
2. The underlying C + + algorithm requires image data in YUV format
Therefore, it is necessary to convert the image data obtained from the photo mode ARGB8888 to YUV.


In the code above, the byte data is cast to int, and more than 127 of the data will be lost by 256, i.e.:
if (a > 127)
{
A-= 256;
}

The RGB byte array is then converted to the YUV array:



Since the RGB-to-YUV formula here is for the RGB numeric range (0~255), it is necessary to turn the range of the previous RGB values back to the -128~127 (0~255), when the data is less than 0 plus 256.

So the conversion of YUV also between 0~255, and then in the mandatory type conversion to Byte, the last YUV data is the byte data, the range -128~127, such YUV directly to the JNI interface passed to the algorithm inside can.
Note that the algorithm interface for JNI is also byte []. YUV ( -128~127) data is passed to the JNI interface, and the algorithm internal (c + +) automatically resolves to the local unsigned char range (0~255).


PS: In fact, you can directly save the RGB array from the bitmap data, and then no longer convert the data type, with the YUV formula to the YUV, and finally the YUV of the int is cast to a byte array on the line. All rights reserved, offenders must investigate (Welcome reprint and Exchange ([email protected]), reprint must indicate the source)

Android image data passed to some pits in C + +

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.