Back to project page My-Team--Your-Team.
The source code is released under:
MIT License
If you think the Android project My-Team--Your-Team listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.selesse.apps; // w w w .ja va 2 s .co m import android.annotation.TargetApi; import android.content.Context; import android.hardware.Camera; import android.os.Build; import android.view.SurfaceHolder; import android.view.SurfaceView; import com.selesse.apps.activity.CameraActivity; public class Preview extends SurfaceView implements SurfaceHolder.Callback { public SurfaceHolder mHolder; public Camera camera; @SuppressWarnings("deprecation") public Preview(Context context) { super(context); // Install a SurfaceHolder.Callback so we get notified when the // underlying surface is created and destroyed. mHolder = getHolder(); mHolder.addCallback(this); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } // Called once the holder is ready @TargetApi(9) @Override public void surfaceCreated(SurfaceHolder holder) { // The Surface has been created, acquire the camera and tell it where // to draw. try { camera = Camera.open(); // if the camera's null (aka back-facing camera not found), try to // get a front facing one if (camera == null) { if (Build.VERSION.SDK_INT >= 9) { Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); for (int camId = 0; camId < Camera.getNumberOfCameras(); camId++) { Camera.getCameraInfo(camId, cameraInfo); if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { try { camera = Camera.open(camId); } catch (RuntimeException e) { e.printStackTrace(); } } } } } if (camera != null) { camera.setDisplayOrientation(90); camera.setPreviewDisplay(holder); } else { CameraActivity.showNoCameraError(); } } catch (Exception e) { e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null) { camera.stopPreview(); camera.release(); } camera = null; } @Override public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { if (camera != null) { try { camera.startPreview(); } catch (RuntimeException e) { CameraActivity.showNoCameraError(); } } } public boolean hasCameraEnabled() { return camera != null; } }