简体   繁体   中英

how to get RGB values from byte[] array in android camera2 when ImageFormat is JPEG?

I'm using camera2 API I'm interested in getting RGB value from byte array actually average of RGB values can I get it from this part when byte array is collected from ByteBuffer and gets written in file with FileOutputStream.write(bytes)

Here is my code:

ByteBuffer byteBuffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[byteBuffer.remaining()]; 
byteBuffer.get(bytes); 
FileOutputStream fileOutputStream = null; 
try {  
     fileOutputStream = new FileOutputStream(mImageFile);    
     fileOutputStream.write(bytes); 
} 
catch (FileNotFoundException e) {  
   // TODO Auto-generated catch block  e.printStackTrace(); } catch    
   (IOException e) {  
          // TODO Auto-generated catch block  e.printStackTrace(); 
   } 
finally {  
    mImage.close();  
    try {   
       fileOutputStream.close();  
    } 
    catch (IOException e) {   // TODO Auto-generated catch block  
        e.printStackTrace();  
    } 
} 

The Image class reference page implies that this BitmapFactory method might be what you're looking for, to convert the JPEG format data to a Bitmap .

However, if you don't need the data to be in JPEG format for saving to a file as well (ie if you're simply processing images from the camera in the app), you may want to consider changing the output Surface format from JPEG to YUV_420_888 . Then you can convert that to RGB Bitmap (see this answer ) and save the considerable amount of time it takes to encode JPEG.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM