安卓开发,Camera的PreviewCallBack获取的byte[]怎么转为Bitmap?
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励20(财富值+成长值)
byte[] bb=new byte[1024];
Bitmap bit=BitmapFactory.decodeByteArray(bb, 0,bb.length);
不行
格式不对
亲测成功
public Bitmap decodeToBitMap(byte[] data, Camera _camera) {
if(data==null||_camera==null){
return null;
}
Camera.Size size = _camera.getParameters().getPreviewSize();
try {
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width,
size.height, null);
if (image != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new Rect(0, 0, size.width, size.height),
80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(
stream.toByteArray(), 0, stream.size());
stream.close();
return bmp;
}
} catch (Exception ex) {
}
return null;
}