gpt4 book ai didi

java - 选择一个后裁剪图像的最佳方式

转载 作者:行者123 更新时间:2023-12-02 09:43:09 26 4
gpt4 key购买 nike

需要裁剪图像的最佳方法

尝试了一些方法,但它没有给我正确裁剪的选项,如果可以的话请帮助我提供示例和裁剪图像的最佳方法

private void selectFile() {
Intent img = new Intent(Intent.ACTION_OPEN_DOCUMENT);
img.setType("image/*");
startActivityForResult(i, 3000);
}

--- 穆罕默德 ---如何获取裁剪后的Uri

                Uri uri = data.getData();
getContentResolver().takePersistableUriPermission(Objects.requireNonNull(uri), Intent.FLAG_GRANT_READ_URI_PERMISSION);
stickerPack.addSticker(uri, this);
}
finish();
startActivity(getIntent());
}

最佳答案

您可以使用默认的 Android 裁剪功能

int PICK_IMAGE_REQUEST = 100;
int PIC_CROP = 1;

void chooseImageFromGallery() {
if (isPermissionsGranted(Manifest.permission.READ_EXTERNAL_STORAGE)) {
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE),
PICK_IMAGE_REQUEST);
}
}
}

@Override void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PICK_IMAGE_REQUEST && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED ) {
chooseImageFromGallery()
}
}


@Override void onActivityResult(Int requestCode, Int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
Uri imageUri = data?.data
performCrop(imageUri)
}else if(resultCode == Activity.RESULT_OK && requestCode == PIC_CROP){
// get the returned data
Bundle extras = data.getExtras();
// get the cropped bitmap
Bitmap selectedBitmap = extras.getParcelable("data");

imgView.setImageBitmap(selectedBitmap);
}
}

private void performCrop(Uri picUri) {
try {
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(picUri, "image/*");
// set crop properties here
cropIntent.putExtra("crop", true);
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 128);
cropIntent.putExtra("outputY", 128);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, PIC_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}

或者您可以使用任何库来选取和裁剪图像,请检查这些库

https://github.com/ArthurHub/Android-Image-Cropper

https://github.com/Yalantis/uCrop

关于java - 选择一个后裁剪图像的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56893553/

26 4 0
文章推荐: java - 一旦返回某些内容,程序是否会停止执行?
文章推荐: java - 如何在 Android 上选择/强制使用移动数据(或 WiFi)进行网络通话?
文章推荐: java - 如何指定 AutoWired Bean 的子依赖项
文章推荐: java - 在 Android Room 数据库中存储 List