/** Uninitialize camera. */ private void uninitializeCamera() { // - If camera is initialized, uninitialied it. if (mEasyCamera != null) { mEasyCamera.stopPreview(); mEasyCamera.close(); } }
@Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { // our app has only one screen, so we'll destroy the camera in the surface // if you are unsing with more screens, please move this code your activity mCamera.stopPreview(); mCamera.close(); }
@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()); } }
/** Initialize camera with all good paramters. */ private void initializeCamera() { // - Open camera. mEasyCamera = DefaultEasyCamera.open(); // - Make sure preview is not started. mEasyCamera.stopPreview(); // - Set good parameters. Camera.Parameters params = mEasyCamera.getParameters(); params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); mEasyCamera.setParameters(params); mEasyCamera.setDisplayOrientation(90); }
@Override public void onCropping() { // - Update snap icon, display progress circle and stop preview. mSnapButton.setImageResource(R.drawable.ic_visibility_white_24dp); mFabProgressCircle.show(); mEasyCamera.stopPreview(); }
@SuppressWarnings("deprecation") public EasyCameraView(Context context, EasyCamera camera) { super(context); final Context context1 = getContext(); mCamera = camera; mCamera.setDisplayOrientation(90); mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes(); for (Camera.Size str : mSupportedPreviewSizes) Log.e(TAG, str.width + "/" + str.height); // get the holder and set this class as the callback, so we can get camera data here mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); callback = new PictureCallback() { public void onPictureTaken(byte[] data, EasyCamera.CameraActions actions) { Log.d("Log", "onPictureTaken - wrote bytes: " + data.length); File pictureFileDir = getDir(); if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) { Log.d("Log", "Can't create directory to save image."); return; } SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss"); String date = dateFormat.format(new Date()); String photoFile = "Picture_" + date + ".jpg"; String filename = pictureFileDir.getPath() + File.separator + photoFile; File pictureFile = new File(filename); try { FileOutputStream fos = new FileOutputStream(pictureFile); fos.write(data); fos.close(); Toast.makeText(context1, "New Image saved:" + filename, Toast.LENGTH_LONG).show(); Log.d("Log", "New Image saved:" + filename); } catch (Exception error) { Log.d("Log", "File" + filename + "not saved: " + error.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); } }
/** * Get camera preview's ratio. * * @return Preview's ratio. */ private float getCameraPreviewRatio() { final Camera.Size size = mEasyCamera.getParameters().getPreviewSize(); return (float) size.width / size.height; }