Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Preview extends ViewGroup implements SurfaceHolder.Callback {
- private final String TAG = "Preview";
- SurfaceView mSurfaceView;
- SurfaceHolder mHolder;
- Camera.Size mPreviewSize;
- List<Camera.Size> mSupportedPreviewSizes;
- Activity mAcivity;
- Camera mCamera;
- public int currentCameraId;
- Preview(Context context, SurfaceView sv, Activity activity) {
- super(context);
- mSurfaceView = sv;
- mAcivity = activity;
- // addView(mSurfaceView);
- mHolder = mSurfaceView.getHolder();
- mHolder.addCallback(this);
- mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
- }
- public void setCamera(Camera camera) {
- mCamera = camera;
- if (mCamera != null) {
- mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes();
- requestLayout();
- // get Camera parameters
- Camera.Parameters params = mCamera.getParameters();
- List<String> focusModes = params.getSupportedFocusModes();
- if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
- // set the focus mode
- params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
- // set Camera parameters
- mCamera.setParameters(params);
- }
- }
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
- final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);
- setMeasuredDimension(width, height);
- if (mSupportedPreviewSizes != null) {
- mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height);
- }
- }
- @Override
- protected void onLayout(boolean changed, int l, int t, int r, int b) {
- if (changed && getChildCount() > 0) {
- final View child = getChildAt(0);
- final int width = r - l;
- final int height = b - t;
- int previewWidth = width;
- int previewHeight = height;
- if (mPreviewSize != null) {
- previewWidth = mPreviewSize.width;
- previewHeight = mPreviewSize.height;
- }
- // Center the child SurfaceView within the parent.
- if (width * previewHeight > height * previewWidth) {
- final int scaledChildWidth = previewWidth * height / previewHeight;
- child.layout((width - scaledChildWidth) / 2, 0,
- (width + scaledChildWidth) / 2, height);
- } else {
- final int scaledChildHeight = previewHeight * width / previewWidth;
- child.layout(0, (height - scaledChildHeight) / 2,
- width, (height + scaledChildHeight) / 2);
- }
- }
- }
- public void surfaceCreated(SurfaceHolder holder) {
- try {
- if (mCamera != null) {
- mCamera.setPreviewDisplay(holder);
- Camera.CameraInfo info = new Camera.CameraInfo();
- Camera.getCameraInfo(currentCameraId, info);
- if (info.canDisableShutterSound) {
- SharedPreferences prefs = mAcivity.getSharedPreferences("myprefs", Context.MODE_PRIVATE);
- mCamera.enableShutterSound(prefs.getBoolean("shutter",true));
- }
- Camera.Parameters parameters = mCamera.getParameters();
- CameraActivity.setParams(parameters.getPreviewSize());
- }
- } catch (IOException exception) {
- Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
- }
- }
- public void surfaceDestroyed(SurfaceHolder holder) {
- if (mCamera != null) {
- mCamera.stopPreview();
- }
- }
- private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
- final double ASPECT_TOLERANCE = 0.1;
- double targetRatio = (double) w / h;
- if (sizes == null) return null;
- Camera.Size optimalSize = null;
- double minDiff = Double.MAX_VALUE;
- int targetHeight = h;
- for (Camera.Size size : sizes) {
- double ratio = (double) size.width / size.height;
- if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;
- if (Math.abs(size.height - targetHeight) < minDiff) {
- optimalSize = size;
- minDiff = Math.abs(size.height - targetHeight);
- }
- }
- if (optimalSize == null) {
- minDiff = Double.MAX_VALUE;
- for (Camera.Size size : sizes) {
- if (Math.abs(size.height - targetHeight) < minDiff) {
- optimalSize = size;
- minDiff = Math.abs(size.height - targetHeight);
- }
- }
- }
- return optimalSize;
- }
- public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
- if (mCamera != null) {
- Camera.Parameters parameters = mCamera.getParameters();
- requestLayout();
- List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
- Camera.Size previewSize =previewSizes.get(0);
- parameters.setPreviewSize(previewSize.width, previewSize.height);
- mCamera.setParameters(parameters);
- mCamera.setDisplayOrientation(getRoatationAngle(mAcivity, currentCameraId));
- mCamera.startPreview();
- }
- }
- public static int getRoatationAngle(Activity mContext, int cameraId) {
- android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
- android.hardware.Camera.getCameraInfo(cameraId, info);
- int rotation = mContext.getWindowManager().getDefaultDisplay().getRotation();
- int degrees = 0;
- switch (rotation) {
- case Surface.ROTATION_0:
- degrees = 0;
- break;
- case Surface.ROTATION_90:
- degrees = 90;
- break;
- case Surface.ROTATION_180:
- degrees = 180;
- break;
- case Surface.ROTATION_270:
- degrees = 270;
- break;
- }
- int result;
- if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
- result = (info.orientation + degrees) % 360;
- result = (360 - result) % 360; // compensate the mirror
- } else { // back-facing
- result = (info.orientation - degrees + 360) % 360;
- }
- return result;
- }
- public void setCurrnatCamera(int id) {
- if (id != -1) {
- currentCameraId = id;
- } else {
- currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK;
- }
- }
- }
- <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/FrameLayout01"
- android:layout_width="fill_parent"
- android:background="?attr/colorPrimary"
- android:layout_height="fill_parent">
- <RelativeLayout
- android:id="@+id/rl1"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <FrameLayout
- android:id="@+id/previewContainer"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <SurfaceView
- android:id="@+id/SurfaceView01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- />
- </FrameLayout>
- <RelativeLayout
- android:id="@+id/handle"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_alignParentBottom="true"
- android:layout_gravity="center_horizontal|bottom"
- android:background="?attr/colorPrimary"
- android:orientation="horizontal"
- android:padding="5dp"
- >
- <ImageButton
- android:id="@+id/button"
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:layout_centerInParent="true"
- android:scaleType="fitXY"
- android:background="@drawable/photo"
- />
- <ImageButton
- android:id="@+id/recam"
- android:layout_width="50dp"
- android:layout_height="50dp"
- android:background="@drawable/change"
- android:layout_centerVertical="true"
- />
- </RelativeLayout>
- </RelativeLayout>
- </FrameLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement