我想点击开始按钮,显示相机预览。
但不行。
我使用setPreviewDisplay
startPreview
发生RuntimeException: Fail to connect to camera service
public class AndroidCamera extends Activity implements SurfaceHolder.Callback {
private static Camera camera = null;
private Camera camera;
private static Camera camera2 = null;
SurfaceView surfaceView;
SurfaceView surfaceView2;
SurfaceHolder surfaceHolder;
SurfaceHolder surfaceHolder2;
Button buttonStartCameraPreview, buttonStopCameraPreview;
boolean previewing = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonStartCameraPreview = (Button) findViewById(R.id.startcamerapreview);
buttonStopCameraPreview = (Button) findViewById(R.id.stopcamerapreview);
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
buttonStartCameraPreview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!previewing) {
camera = Camera.open(0);
if (camera != null) {
try {
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview(); //occur exception
previewing = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
我使用清单权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus"/>
感谢 请给我建议