我有一个位图,我正在为它分配一个字节数组值,使用代码:
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,onclicc的按钮如下:
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。