第一部分:
由于Android下摄像头预览数据只能 ImageFormat.NV21 格式的,所以解码时要经过一翻周折.
Camera mCamera = Camera.open();
Camera.Parameters p = mCamera.getParameters();
p.setPreviewFormat(ImageFormat.NV21);
mCamera.setParameters(p);
mCamera.startPreview();
下面是解码核心部分:
@Override
publicvoidonPreviewFrame(byte[] data, Camera camera) {
Size size = camera.getParameters().getPreviewSize();
try{
YuvImage image = newYuvImage(data, ImageFormat.NV21, size.width, size.height,null);
if(image!=null){
ByteArrayOutputStream stream = newByteArrayOutputStream();
image.compressToJpeg(newRect(0,0, size.width, size.height),80, stream);
Bitmap bmp = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
stream.close();
}
}catch(Exception ex){