这是我的所有代码(没有太多,所以我认为我粘贴了):
问题是我基本上从教程中复制了所有这些代码。除了我从here获得的decodeYUV
方法。
我的手机屏幕分辨率为480x800
。
我遇到问题的关键方法是带有字节流的onPictureTaken
方法。
问题出在我拍照时。位图如下所示:
public class HuntActivity extends AppCompatActivity implements SurfaceHolder.Callback{
Camera camera;
SurfaceView surfaceView;
SurfaceHolder surfaceHolder;
Camera.PictureCallback jpegCallback;
ImageView sbut;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hunt);
sbut = (ImageView) findViewById(R.id.searchbut);
surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
jpegCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
int[] rgbs = new int[480*800*3/2]; //buffer size
decodeYUV(rgbs, data, 480, 800);
Bitmap bitmap = Bitmap.createBitmap(rgbs, 480, 800, Bitmap.Config.ARGB_8888);
sbut.setImageBitmap(bitmap);
Toast.makeText(getApplicationContext(), "Picture Saved", 2000).show();
refreshCamera();
}
};
}
public void captureImage(View v) throws IOException {
camera.takePicture(null, null, jpegCallback);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {camera = Camera.open();
} catch (RuntimeException e) {
System.err.println(e);
return;
}
Camera.Parameters param;
param = camera.getParameters();
param.setPreviewSize(800, 480);
camera.setDisplayOrientation(90);
camera.setParameters(param);
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {
System.err.println(e);
return;
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
camera.stopPreview();
camera.release();
camera = null;
}
public void refreshCamera() {
if (surfaceHolder.getSurface() == null) {return;}
try {camera.stopPreview();
} catch (Exception e){}
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();
} catch (Exception e) {}
}
...
...
答案 0 :(得分:1)
实现jpegCallback
,然后将数据字节视为YUV编码。不。数据字节包含jpg图像。所以请相应地对待它们。