我正在尝试创建一个非常简单的应用程序,显示从显示器上的相机收到的图片.我正在使用camera2 API,因为我是新手,所以我尝试使用最新的技术.

我达到了我称之为createCaptureSession()的程度,但随后onConfigureFailed()被调用,我不知道问题是什么或如何找到它.

这是我的来源:

SurfaceView mSurfaceView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

Log.d(TAG,"onCreate() start");

setContentView(R.layout.activity_main);

mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);

}

protected void onResume() {

super.onResume();

Log.d(TAG,"onResume() start");

initCamera();

}

private void initCamera() {

Log.d(TAG,"initCamera() start");

CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);

Log.d(TAG,"acquired cameraManager: " + cameraManager);

String[] cameraIdList;

try {

cameraIdList = cameraManager.getCameraIdList();

} catch (CameraAccessException e) {

Log.e(TAG,"couldn't get camera list",e);

return;

}

Log.d(TAG,"acquired cameraIdList: length: " + cameraIdList.length);

if (cameraIdList.length == 0) {

Log.w(TAG,"couldn't detect a camera");

return;

}

String camera0Id = cameraIdList[0];

Log.d(TAG,"chosen camera: " + camera0Id);

try {

cameraManager.openCamera(camera0Id,deviceCallback,null);

} catch (CameraAccessException e) {

Log.e(TAG,"couldn't open camera",e);

}

Log.d(TAG,"called cameraManager.openCamera()");

}

CameraDevice.StateCallback deviceCallback = new CameraDevice.StateCallback() {

@Override

public void onOpened(CameraDevice camera) {

Log.d(TAG,"deviceCallback.onOpened() start");

Surface surface = mSurfaceView.getHolder().getSurface();

Log.d(TAG,"surface: " + surface);

List surfaceList = Collections.singletonList(surface);

try {

camera.createCaptureSession(surfaceList,sessionCallback,null);

} catch (CameraAccessException e) {

Log.e(TAG,"couldn't create capture session for camera: " + camera.getId(),e);

return;

}

}

@Override

public void onDisconnected(CameraDevice camera) {

Log.d(TAG,"deviceCallback.onDisconnected() start");

}

@Override

public void onError(CameraDevice camera,int error) {

Log.d(TAG,"deviceCallback.onError() start");

}

};

CameraCaptureSession.StateCallback sessionCallback = new CameraCaptureSession.StateCallback() {

@Override

public void onConfigured(CameraCaptureSession session) {

Log.i(TAG,"capture session configured: " + session);

}

@Override

public void onConfigureFailed(CameraCaptureSession session) {

Log.e(TAG,"capture session configure failed: " + session);

}

};

输出是:

...﹕ onCreate() start

...﹕ onResume() start

...﹕ initCamera() start

...﹕ acquired cameraManager: android.hardware.camera2.CameraManager@5d68786

...﹕ acquired cameraIdList: length: 2

...﹕ chosen camera: 0

...﹕ called cameraManager.openCamera()

...﹕ deviceCallback.onOpened() start

...﹕ surface: Surface(name=null)/@0x52c91e3

...﹕ capture session configure failed: android.hardware.camera2.impl.CameraCaptureSessionImpl@1a8c7a99

以下是我的AndroidManifest.xml:

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐