How Can I Show My Camera Preview
I wrote a class for camera preview public class CameraView extends SurfaceView implements SurfaceHolder.Callback{ private Camera camera; public CameraView(Context context
Solution 1:
Use FrameLayout and add CameraPreview to it.
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = newCameraPreview(this, mCamera);
FrameLayoutpreview= (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
/** A basic Camera preview class */publicclassCameraPreviewextendsSurfaceViewimplementsSurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
publicCameraPreview(Context context, Camera camera) {
super(context);
mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the// underlying surface is created and destroyed.
mHolder = getHolder();
mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
publicvoidsurfaceCreated(SurfaceHolder holder) {
// The Surface has been created, now tell the camera where to draw the preview.try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
Log.d(TAG, "Error setting camera preview: " + e.getMessage());
}
}
publicvoidsurfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity.
}
publicvoidsurfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here.// Make sure to stop the preview before resizing or reformatting it.if (mHolder.getSurface() == null){
// preview surface does not existreturn;
}
// stop preview before making changestry {
mCamera.stopPreview();
} catch (Exception e){
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or// reformatting changes here// start preview with new settingstry {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.d(TAG, "Error starting camera preview: " + e.getMessage());
}
}
}
<FrameLayout
android:id="@+id/camera_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
/>
Solution 2:
try this:
You need to Change in your SurfaceView in XML File.
Replace your surfaceView with this
<CameraView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/camera_view" />
and your remaining code is Right.let me know after change
Hope this will helps...(:
Solution 3:
At first you should add the camera permission in you Manifest.xml
:
android:name="android.permission.CAMERA
And, add this in your activity :
@OverrideprotectedvoidonResume() {
super.onResume();
mCameraView.start();
}
@OverrideprotectedvoidonPause() {
mCameraView.stop();
super.onPause();
}
Check this link, it's the easiest way to do it : https://github.com/google/cameraview
Post a Comment for "How Can I Show My Camera Preview"