0

I'm try to get a byte array from a picture stored on an android device, but the byte array size is width * heigth * 2, and I need a byte array of the size width * heigth * 3.

My problem is that I need to send a picture byte array to a matlab server that need the array in this RGB format.

Here is the code that creates a byte array of "width * height" size:

            Bitmap photo = BitmapHelper.readBitmap(CameraIntentActivity.this, photoUri);
            if (photo != null) {
                photo = BitmapHelper.shrinkBitmap(photo, 300, rotateXDegrees);

                Bitmap bm = Bitmap.createScaledBitmap(photo, 640, 480, true);

                final int lnth= bm.getByteCount();
                ByteBuffer dst= ByteBuffer.allocate(lnth);
                bm.copyPixelsToBuffer( dst);
                byte[] byteArray = dst.array();

The readBitmap method:

public static Bitmap readBitmap(Context context, Uri selectedImage) {
    Bitmap bm = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inScaled = false;
    AssetFileDescriptor fileDescriptor = null;
    try {
        fileDescriptor = context.getContentResolver().openAssetFileDescriptor(selectedImage, "r");
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        try {
            bm = BitmapFactory.decodeFileDescriptor(
                    fileDescriptor.getFileDescriptor(), null, options);
            fileDescriptor.close();
        } catch (IOException e) {
            return null;
        }
    }
    return bm;
}
  • What is the length of byteArray? 640*480 bytes? Or 648*480*4 bytes? – greenapps Nov 02 '16 at 20:51
  • I'll bet your `lnth` value is actually w * h * 4. Your bitmap config is most likely ARGB_8888, which means there's an extra byte for the alpha channel value. I'll also bet that what you are looking for is RGB 888, which is a config that Android doesn't support. You will need to copy the RGB values into another array minus the alpha bytes to get w * h * 3. – kris larson Nov 02 '16 at 21:02
  • The actually length is 640*480*2, but I need 640*480*3. The readBitmap uses this config: BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; – diegosoaresub Nov 02 '16 at 21:05
  • Then why did you write `the code that creates a byte array of "width * height" size:`? – greenapps Nov 02 '16 at 21:11
  • Sorry, is my fault, I will fix it – diegosoaresub Nov 02 '16 at 21:13
  • You should of course tell right on the start of your post that you used RGB_565. Give decent info! – greenapps Nov 02 '16 at 21:15
  • OP should have found: http://stackoverflow.com/questions/2442576/how-does-one-convert-16-bit-rgb565-to-24-bit-rgb888 – Morrison Chang Nov 02 '16 at 21:18
  • Bad idea to expand two bytes to three bytes pro pixel. Better start with the normal four ARGB bytes and take the three RGB bytes of them to construct an array as @kris larson already suggested. – greenapps Nov 02 '16 at 21:24
  • Thanks, I will try it. – diegosoaresub Nov 02 '16 at 21:30

0 Answers0