簡體   English   中英

用字節數組初始化時,位圖返回null

[英]Bitmap returning null when initialised with byte array

我有位圖,我正在使用代碼分配字節數組值:

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);

        }
    }

其余代碼工作良好,並且該方法像應被連續調用一樣,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));
        }

Logcat(很多次應該這樣): length of bytes:460800 The bitmap:null

因此,圖像不會設置為位圖為空,實際的功能是將圖像視圖設置為在分配位圖時將要發生的瞬間拍攝的圖片。 我要去哪里錯了?

嘗試一下:

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

返回解碼的位圖;如果無法解碼圖像,則返回null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM