0

I have Bitmap that I'm assigning a byte array value, using code:

public class LegacyCameraManager implements Camera.ErrorCallback, Camera.PreviewCallback, Camera.AutoFocusCallback, Camera.PictureCallback {
public static Bitmap mBitmap;
    @Override
    public void onPreviewFrame(byte[] bytes, Camera camera) {
        Boolean isProcessing = UserSharedPref.initializeSharedPreferencesForprocessFrames(mContext).getBoolean(UserSharedPref.processFrames, true);
        if (isProcessing) {
            Log.d("TEST:","length of bytes:"+bytes.length);
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            mBitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length,options);
            Log.d("TEST:","The bitmap:"+mBitmap);
            tfDetector.onPreviewFrame(bytes, camera);

        }
    }

The rest of the code works well and this method gets called continuously like it should, the thing is mBitmap is logging out as "null" always and this variable has to be set to an imageview like this,onclikc of a button like this:

 if (LegacyCameraManager.mBitmap == null) {
            Log.d("TEST:","Bitmaps is NULL!!");
            img.setImageResource(R.drawable.office);
        } else {
            img.setImageBitmap(Bitmap.createScaledBitmap(LegacyCameraManager.mBitmap, img.getWidth(),
                    img.getHeight(), false));
        }

The Logcat (a lot of number of times like it should): length of bytes:460800 The bitmap:null

And therefore, the image is not getting set to the bitmap being null, the actual function is to set the image view to a picture taken at the instant which will happen of the bitmap is assigned like it is supposed to. Where am I going wrong?

Sonic_
  • 347
  • 3
  • 9
  • I would recommend try to not provide any option into the decodeByteArray. – Ivan Nov 13 '18 at 11:30
  • yes, tried without the option as well....same.....null – Sonic_ Nov 13 '18 at 11:32
  • if `onPreviewFrame(byte[] data, Camera camera)` is from camera.previewCallback, then you have to convert those `byte[] data` to yuv then to bitmap check this link https://stackoverflow.com/questions/4768165/converting-preview-frame-to-bitmap – dardan.g Nov 13 '18 at 12:00
  • `decodeByteArray()` takes a compressed image (.jpg, .png, etc.). The frames passed to `onPreviewFrame()` are not compressed; they are simply in a different format (whatever format you specified when setting up the capture). – greeble31 Nov 13 '18 at 20:16
  • Hey, thanks a lot @d.gjinovci your suggestion worked for me. – Sonic_ Nov 14 '18 at 06:48

1 Answers1

-1

Just try this:

Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0, bitmapdata.length);

Returns the decoded Bitmap, or null if the image could not be decoded.

Sultan Mahmud
  • 1,245
  • 8
  • 18
  • "mBitmap" has to be a global variable. As it is also being used by another activity. It cant be a local object. – Sonic_ Nov 13 '18 at 11:44
  • Local or Public that's not your problem. I think you should learn basic of java like Access Modifier. Thanks – Sultan Mahmud Nov 13 '18 at 11:47
  • This does not address the basic problem: That the input format is not compressed, and therefor `decodeByteArray()` is not appropriate to begin with. – greeble31 Nov 13 '18 at 20:23
  • opps. I overlooked that thing. ok Then try this: ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray); Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream); – Sultan Mahmud Nov 13 '18 at 20:31