I want to take a picture from camera then doing auto crop once image captured. Is there an example of source code? I feel hard to find the example

- 1,852
- 12
- 28

- 511
- 4
- 9
- 20
2 Answers
This is not a simple answer, as it will take quite a bit of time and learning to get what you want up and running. I would recommend looking into OpenCv - it is a very powerful tool, and can do loads more image processing than just cropping, but something like cropping can be done very easily.
This link shows an example of cropping framed art automatically using OpenCv. It would be a good reference to look at for your project, since what you are wanting to crop is rectangular and has a standard shape. If that is not the case, then you would need to dive more into the image processing techniques of detecting the specific object you want to crop. There are many more resources online (including here on StackExchange) for learning OpenCv.
Also, this post has some good references for learning how to implement OpenCv on Android.
Best of luck!

- 764
- 1
- 8
- 17
i suggest you to use this library Android-Image-Cropper by ArthurHub, it gives you a plenty of cropping choices (shape , size ,....) , and it also give you the choice of taking new picture or using one from the gallery
int myColor;
if(Build.VERSION.SDK_INT >= 21){
myColor = ContextCompat.getColor(this, R.color.white);
}
else{
myColor=getResources().getColor(R.color.white);
}
CropImage.activity()
.setActivityMenuIconColor(myColor)
.setAllowRotation(true)
.setFixAspectRatio(true)
.setAspectRatio(3, 2)
.setCropShape(CropImageView.CropShape.RECTANGLE)
.setActivityTitle("Selection d'image")
.start(this);
and you have to handle the result :
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE){
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
Uri resultUri = result.getUri();
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), resultUri);
String string = resultUri.toString();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
} catch (IOException e) {
e.printStackTrace();
}
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Exception error = result.getError();
}
}
}

- 5,748
- 2
- 21
- 38
-
I'll try it first – andika_kurniawan Apr 22 '19 at 16:06
-
For sure , im using it on more than 3 applications and it works like a charm :) – ismail alaoui Apr 22 '19 at 16:07