简体   繁体   English

用字节数组初始化时,位图返回null

[英]Bitmap returning null when initialised with byte array

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: 其余代码工作良好,并且该方法像应被连续调用一样,mBitmap始终以“ null”注销,并且必须将此变量设置为如下所示的imageview,如下所示:onclikc

 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 Logcat(很多次应该这样): 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? 我要去哪里错了?

Just try this: 尝试一下:

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

Returns the decoded Bitmap, or null if the image could not be decoded. 返回解码的位图;如果无法解码图像,则返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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