Cropping Captured Image Is Blur In Android
Hi basically I am selecting image form gallery or capture from camera and cropping it. If I crop image which is selected from gallery is no blur its fine but if I crop captured i
Solution 1:
You can use a library to crop image, that would give proper results along with the image quality. use: compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
And for more you can visit this page: check my answer
Solution 2:
This answer will helps others to resolve issue on capture photo from camera which gives a blur image while cropping.
By adding library in build.gradle
:
implementation 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
Adding Permission on Manifest
:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
CameraActivity.java :
publicclassCameraActivityextendsActivity {
private CropImageView mCropImageView;
private CircleImageView imVCature_pic;
private Uri mCropImageUri;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
mCropImageView = (CropImageView) findViewById(R.id.CropImageView);
imVCature_pic = (CircleImageView) findViewById(R.id.imVCature_pic);
}
/**
* On load image button click, start pick image chooser activity.
*/publicvoidonLoadImageClick(View view) {
startActivityForResult(getPickImageChooserIntent(), 200);
}
/**
* Crop the image and set it back to the cropping view.
*/publicvoidonCropImageClick(View view) {
Bitmapcropped= mCropImageView.getCroppedImage(500, 500);
if (cropped != null)
imVCature_pic.setImageBitmap(cropped);
//mCropImageView.setImageBitmap(cropped);
}
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
UriimageUri= getPickImageResultUri(data);
// For API >= 23 we need to check specifically that we have permissions to read external storage,// but we don't know if we need to for the URI so the simplest is to try open the stream and see if we get error.booleanrequirePermissions=false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
isUriRequiresPermissions(imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
requirePermissions = true;
mCropImageUri = imageUri;
requestPermissions(newString[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
}
if (!requirePermissions) {
mCropImageView.setImageUriAsync(imageUri);
}
}
}
@OverridepublicvoidonRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (mCropImageUri != null && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mCropImageView.setImageUriAsync(mCropImageUri);
} else {
Toast.makeText(CameraActivity.this, "Required permissions are not granted", Toast.LENGTH_LONG).show();
}
}
/**
* Create a chooser intent to select the source to get image from.<br/>
* The source can be camera's (ACTION_IMAGE_CAPTURE) or gallery's (ACTION_GET_CONTENT).<br/>
* All possible sources are added to the intent chooser.
*/public Intent getPickImageChooserIntent() {
// Determine Uri of camera image to save.UrioutputFileUri= getCaptureImageOutputUri();
List<Intent> allIntents = newArrayList<>();
PackageManagerpackageManager= getPackageManager();
// collect all camera intentsIntentcaptureIntent=newIntent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for (ResolveInfo res : listCam) {
Intentintent=newIntent(captureIntent);
intent.setComponent(newComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
// collect all gallery intentsIntentgalleryIntent=newIntent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
for (ResolveInfo res : listGallery) {
Intentintent=newIntent(galleryIntent);
intent.setComponent(newComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
allIntents.add(intent);
}
// the main intent is the last in the list (fucking android) so pickup the useless oneIntentmainIntent= allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
// Create a chooser from the main intentIntentchooserIntent= Intent.createChooser(mainIntent, "Select source");
// Add all other intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(newParcelable[allIntents.size()]));
return chooserIntent;
}
/**
* Get URI to image received from capture by camera.
*/private Uri getCaptureImageOutputUri() {
UrioutputFileUri=null;
FilegetImage= getExternalCacheDir();
if (getImage != null) {
outputFileUri = Uri.fromFile(newFile(getImage.getPath(), "pickImageResult.jpeg"));
}
return outputFileUri;
}
/**
* Get the URI of the selected image from {@link #getPickImageChooserIntent()}.<br/>
* Will return the correct URI for camera and gallery image.
*
* @param data the returned data of the activity result
*/public Uri getPickImageResultUri(Intent data) {
booleanisCamera=true;
if (data != null && data.getData() != null) {
Stringaction= data.getAction();
isCamera = action != null && action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
}
return isCamera ? getCaptureImageOutputUri() : data.getData();
}
/**
* Test if we can open the given Android URI to test if permission required error is thrown.<br>
*/publicbooleanisUriRequiresPermissions(Uri uri) {
try {
ContentResolverresolver= getContentResolver();
InputStreamstream= resolver.openInputStream(uri);
stream.close();
returnfalse;
} catch (FileNotFoundException e) {
if (e.getCause() instanceof Exception) {
returntrue;
}
} catch (Exception e) {
e.printStackTrace();
}
returnfalse;
}
}
activity_camera.xml :
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="onLoadImageClick"android:padding="@dimen/activity_horizontal_margin"android:text="Load Image" /><com.theartofdev.edmodo.cropper.CropImageViewandroid:id="@+id/CropImageView"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="5"android:scaleType="fitCenter" /><Buttonandroid:layout_width="match_parent"android:layout_height="wrap_content"android:onClick="onCropImageClick"android:padding="@dimen/activity_horizontal_margin"android:text="Crop Image" /></LinearLayout>
Post a Comment for "Cropping Captured Image Is Blur In Android"