@Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { Log.e("ok", "surfaceChanged => w=" + i2 + ", h=" + i3); // before changing the application orientation, you need to stop the preview, rotate and then // start it again if (surfaceHolder.getSurface() == null) // check if the surface is ready to receive camera data return; try { mCamera.stopPreview(); } catch (Exception e) { // this will happen when you are trying the camera if it's not running } // now, recreate the camera preview try { Camera.Parameters parameters = mCamera.getParameters(); List<String> focusModes = parameters.getSupportedFocusModes(); if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO)) { parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO); } parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); mCamera.setParameters(parameters); mCamera.setDisplayOrientation(90); mCamera.startPreview(mHolder); } catch (IOException e) { Log.d("ERROR", "Camera error on surfaceChanged " + e.getMessage()); } }
@Override public void surfaceCreated(SurfaceHolder surfaceHolder) { try { // when the surface is created, we can set the camera to draw images in this surfaceholder actions = mCamera.startPreview(mHolder); } catch (IOException e) { Log.d("ERROR", "Camera error on surfaceCreated " + e.getMessage()); } }
/** * Initialize preview with good ratio. * * @param surfaceTexture */ private void initializePreview(final SurfaceTexture surfaceTexture) { try { // - Retrieve camera ratio then fit preview and cropper. final float ratio = getCameraPreviewRatio(); updatePreviewRatio(ratio); fitCropImageView(ratio); // - If snap button is still enable, start preview. if (mSnapButton.isEnabled()) { mEasyCamera.startPreview(surfaceTexture); } } catch (final Throwable cause) { // - An exception may be thrown when camera is unavailable. displayCameraError(cause); } }