'Crop the images from camera screen android
I want to develop a camera screen which consists of a camera with the frame (see the first image). When I take the photo I don't need the full image, I need only the portion of the image present inside the frame (see the second image).
I tried many image crop libraries in github.
https://github.com/Yalantis/uCrop
https://github.com/ArthurHub/Android-Image-Cropper
I also tried with intent to call crop
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(uri, "image/*");
cropIntent.putExtra("crop", "true");
cropIntent.putExtra("scale", true);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE);
These are not helping me to crop the image inside the frame. I don't want crop screen separately. Please help me out. Thanks in advance.
Solution 1:[1]
Well. Try this.
int[] location = new int[2];
iv_frame.getLocationOnScreen(location);
int x = location[0];
int y = location[1];
Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, x, y, iv_frame.getMeasuredWidth() + 160, iv_frame.getMeasuredHeight() + 160); // Adjust the 160 according to your need. In my case the frame has a fixed length and width
Take picture -> Call this function with the camera result data -> Save the resulting bitmap :)
Solution 2:[2]
You can create a custom camera using surface and try this hope result is as you want
public class MainActivity extends Activity implements SurfaceHolder.Callback {
Camera mCamera;
SurfaceView mPreview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPreview = (SurfaceView)findViewById(R.id.preview);
mPreview.getHolder().addCallback(this);
mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
mCamera = Camera.open();
}
@Override
public void onPause() {
super.onPause();
mCamera.stopPreview();
}
@Override
public void onDestroy() {
super.onDestroy();
mCamera.release();
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Parameters parameters = mCamera.getParameters();
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
Camera.Size previewSize = previewSizes.get(4); //480h x 720w
parameters.setPreviewSize(previewSize.width, previewSize.height);
parameters.setFlashMode(Parameters.FLASH_MODE_AUTO);
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
mCamera.setParameters(parameters);
Display display = ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
if(display.getRotation() == Surface.ROTATION_0) {
mCamera.setDisplayOrientation(90);
} else if(display.getRotation() == Surface.ROTATION_270) {
mCamera.setDisplayOrientation(180);
}
mCamera.startPreview();
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(mPreview.getHolder());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PREVIEW","surfaceDestroyed");
}
}
and your xml looks like
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<SurfaceView
android:id="@+id/preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="400px"
android:layout_marginTop="300px"
android:layout_marginStart="70px"
android:layout_marginEnd="70px"
android:background="@drawable/border" />
<RelativeLayout
android:id="@+id/rel_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!---Customize your views and button-->
</RelativeLayout>
</FrameLayout>
and your manifests.xml and if you are using above of android version 5.1 add runtime permission or you can check by manually grant permission from app settings
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
and the response is above code
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Santhosh Joseph |
| Solution 2 |



