package com.afollestad.materialcamera.internal; import android.annotation.SuppressLint; import android.content.Context; import android.hardware.Camera; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; @SuppressWarnings("deprecation") @SuppressLint("ViewConstructor") class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private final static String TAG = "SF-CameraPreview"; protected final SurfaceHolder mHolder; private final Camera mCamera; private int mRatioWidth = 0; private int mRatioHeight = 0; public CameraPreview(Context context, Camera camera) { super(context); mCamera = camera; mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void surfaceCreated(SurfaceHolder holder) { try { mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (Throwable e) { Log.d(TAG, "Error setting camera preview: " + e.getMessage()); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { mHolder.removeCallback(this); } @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (mHolder.getSurface() == null) return; try { mCamera.stopPreview(); } catch (Exception ignored) { } try { mCamera.setPreviewDisplay(mHolder); mCamera.startPreview(); } catch (Exception e) { Log.d(TAG, "Error starting camera preview: " + e.getMessage()); } } /** * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio * calculated from the parameters. Note that the actual sizes of parameters don't matter, that * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result. * * @param width Relative horizontal size * @param height Relative vertical size */ public void setAspectRatio(int width, int height) { if (width < 0 || height < 0) { throw new IllegalArgumentException("Size cannot be negative."); } mRatioWidth = width; mRatioHeight = height; Log.e("lzf_camerapreview",width+" "+height); requestLayout(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec); int height = MeasureSpec.getSize(heightMeasureSpec); if (0 == mRatioWidth || 0 == mRatioHeight) { Log.e("lzf_cameraonMeasure",width+" "+height); setMeasuredDimension(width, height); } else { if (width < height * mRatioWidth / mRatioHeight) { Log.e("lzf_cameraonMeasure",width+" "+width * mRatioHeight / mRatioWidth); setMeasuredDimension(width, width * mRatioHeight / mRatioWidth); } else { Log.e("lzf_cameraonMeasure",height * mRatioWidth / mRatioHeight+" "+height); setMeasuredDimension(height * mRatioWidth / mRatioHeight, height); } } } }