我做了一個應該顯示相機預覽的表面視圖。但是,當我用我的虛擬Android手機打開它時,我得到了一個方格黑白背景和一個大的移動綠色框,而不是實際的相機圖像。Android相機預覽不能在虛擬手機中工作
下面是surfaceview代碼:
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback{
private Camera camera;
private SurfaceHolder surfaceholder;
public CameraPreview(Context context) {
super(context);
surfaceholder= getHolder();
surfaceholder.addCallback(this);
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Camera.Parameters parameters = camera.getParameters();
parameters.setPreviewSize(width, height);
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
camera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try {
camera.setPreviewDisplay(surfaceholder);
} catch (IOException e) {}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
stopCam();
}
public void stopCam(){
if (camera!=null){
camera.stopPreview();
camera.release();
}
}
}
什麼實際的相機?你有連接攝像頭? –