问题描述
- Android Camera2的使用到底是哪里错了?
代码如下:public class CameraTwoActivity extends AppCompatActivity implements TextureView.SurfaceTextureListener {
@Bind(R.id.textureview)android.view.TextureView mPreviewView;private Handler mHandler;private HandlerThread mThreadHandler;private Size mPreviewSize=null;private CaptureRequest.Builder mPreviewBuilder;@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.camera_activity); ButterKnife.bind(this); mThreadHandler = new HandlerThread(""CAMERA2""); mThreadHandler.start(); mHandler = new Handler(mThreadHandler.getLooper()); mPreviewView.setSurfaceTextureListener(this);}@TargetApi(Build.VERSION_CODES.LOLLIPOP)public void onSurfaceTextureAvailable(SurfaceTexture surface int width int height) { try { //获得CameraManager CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); //获得属性 CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(""0""); //支持的STREAM CONFIGURATION StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); //显示的size mPreviewSize = map.getOutputSizes(SurfaceTexture.class)[0]; //打开相机 if (ActivityCompat.checkSelfPermission(this Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions and then overriding // public void onRequestPermissionsResult(int requestCode String[] permissions // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return; } cameraManager.openCamera(""0"" mCameraDeviceStateCallback mHandler); } catch (Exception e) { e.printStackTrace(); }}@Overridepublic void onSurfaceTextureSizeChanged(SurfaceTexture surface int width int height) {}@Overridepublic boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { return false;}//TextureView.SurfaceTextureListener@Overridepublic void onSurfaceTextureUpdated(SurfaceTexture surface) {}private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() { @Override public void onOpened(CameraDevice camera) { try { startPreview(camera); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onDisconnected(CameraDevice camera) { } @Override public void onError(CameraDevice camera int error) { }};//开始预览,主要是camera.createCaptureSession这段代码很重要,创建会话private void startPreview(CameraDevice camera) throws CameraAccessException { SurfaceTexture texture = mPreviewView.getSurfaceTexture(); texture.setDefaultBufferSize(mPreviewSize.getWidth() mPreviewSize.getHeight()); Surface surface = new Surface(texture); Log.i(getClass().getName()1111""); try { mPreviewBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_PREVIEW); } catch (CameraAccessException e) { Log.i(getClass().getName()2222""); e.printStackTrace(); } Log.i(getClass().getName()3333""); mPreviewBuilder.addTarget(surface); camera.createCaptureSession(Arrays.asList(surface) mSessionStateCallback mHandler);}private CameraCaptureSession.StateCallback mSessionStateCallback = new CameraCaptureSession.StateCallback() { @Override public void onConfigured(CameraCaptureSession session) { try { updatePreview(session); } catch (CameraAccessException e) { e.printStackTrace(); } } @Override public void onConfigureFailed(CameraCaptureSession session) { }};private void updatePreview(CameraCaptureSession session) throws CameraAccessException { session.setRepeatingRequest(mPreviewBuilder.build() null mHandler);}
}
解决方案
As the log said you are trying to get width from a Null object.
texture.setDefaultBufferSize(mPreviewSize.getWidth() mPreviewSize.getHeight());
mPreviewSize = null
解决方案二:
空指针错误 你可以在打印的logcat信息的下面 有一个 你的类的代码行 提示 会 定位到 哪一行的
解决方案三:
楼主,问题解决了没?这个camera2我也不会用。
你的代码里有一句。mThreadHandler = new HandlerThread(""CAMERA2"");
我看到SDK->Samples->media->Camera2Basic里用的是 new HandlerThread(""CameraBackground"")
时间: 2024-10-27 16:52:49