Custom Square Camera - Android
I want to integrate a custom camera. I've created a camera preview and added to the layout CameraPreview mPreview = new CameraPreview(this); LayoutParams previewLayoutParams = n
Solution 1:
Try to implements SurfaceHolder.Callback and use Custom Surface view as you want like this:
publicclassTakePictureextendsActivityimplementsSurfaceHolder.Callback {
// a variable to store a reference to the Image View at the main.xml file// private ImageView iv_image;// a variable to store a reference to the Surface View at the main.xml fileprivate SurfaceView sv;
// a bitmap to display the captured imageprivate Bitmap bmp;
FileOutputStream fo;
// Camera variables// a surface holderprivate SurfaceHolder sHolder;
// a variable to control the cameraprivate Camera mCamera;
// the camera parametersprivate Parameters parameters;
private String FLASH_MODE ;
privatebooleanisFrontCamRequest=false;
/** Called when the activity is first created. */@SuppressWarnings("deprecation")@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_surface_holder);
// check if this device has a cameraif (checkCameraHardware(getApplicationContext())) {
// get the Image View at the main.xml file// iv_image = (ImageView) findViewById(R.id.imageView);// get the Surface View at the main.xml fileBundleextras= getIntent().getExtras();
Stringflash_mode= extras.getString("FLASH");
FLASH_MODE = flash_mode;
booleanfront_cam_req= extras.getBoolean("Front_Request");
isFrontCamRequest = front_cam_req;
sv = (SurfaceView) findViewById(R.id.camera_preview);
// Get a surface
sHolder = sv.getHolder();
// add the callback interface methods defined below as the Surface// View// callbacks
sHolder.addCallback(this);
// tells Android that this surface will have its data constantly// replaced
sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} else {
// display in long period of time
Toast.makeText(getApplicationContext(),
"Your Device dosen't have a Camera !", Toast.LENGTH_LONG)
.show();
}
}
/** Check if this device has a camera */privatebooleancheckCameraHardware(Context context) {
if (context.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// this device has a camerareturntrue;
} else {
// no camera on this devicereturnfalse;
}
}
publicstatic Camera getCameraInstance() {
Camerac=null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
@OverridepublicvoidsurfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// get camera parameters
parameters = mCamera.getParameters();
if (FLASH_MODE == null || FLASH_MODE.isEmpty())
{
FLASH_MODE = "auto";
}
parameters.setFlashMode(FLASH_MODE);
// set camera parameters
mCamera.setParameters(parameters);
mCamera.startPreview();
// sets what code should be executed after the picture is taken
Camera.PictureCallbackmCall=newCamera.PictureCallback() {
@OverridepublicvoidonPictureTaken(byte[] data, Camera camera) {
// decode the data obtained by the camera into a Bitmap
Log.d("ImageTakin", "Done");
mCamera.stopPreview();
// release the camera
mCamera.release();
Toast.makeText(getApplicationContext(),
"Your Picture has been taken !", Toast.LENGTH_LONG)
.show();
finish();
}
};
mCamera.takePicture(null, null, mCall);
}
@OverridepublicvoidsurfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell it where// to draw the preview.
mCamera = getCameraInstance();
try {
mCamera.setPreviewDisplay(holder);
} catch (IOException exception) {
mCamera.release();
mCamera = null;
}
}
}
@OverridepublicvoidsurfaceDestroyed(SurfaceHolder holder) {
}
Then use your Custom Surface View (square view)like This:
<SurfaceView
android:id="@+id/camera_preview"
android:layout_width="..."
android:layout_height="..."
..... />
Post a Comment for "Custom Square Camera - Android"