Android 图像裁剪:输出 x 和输出 y
Android Image Crop: Output x and Output y
我已经实现了具有特定输出大小的图像裁剪意图。但是在代码中对尺寸所做的更改不会对图像产生任何影响,并且每次更改的输出都保持不变。
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
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", 400);
cropIntent.putExtra("outputY", 400);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
输出 x 和 y 值的变化不会影响被裁剪的图像大小。图像大小始终保持 160*160。只是当我改变纵横比时,图像尺寸会发生一点变化。但是我需要为 x 和 y
提供一个特定的大小
如果有任何关于图像裁剪的库并且在所有设备上都支持,那就太棒了
设置 aspectX 和 aspectY 与 outputX 和 outputY 的比率。
假设您希望结果图像为 200 * 300 像素。然后,只需将 aspectX 设置为 2,将 aspectY 设置为 3。您将获得所需的图像尺寸。这是更新后的代码。
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 3);
// indicate output X and Y
cropIntent.putExtra("outputX", 200);
cropIntent.putExtra("outputY", 300);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
我已经实现了具有特定输出大小的图像裁剪意图。但是在代码中对尺寸所做的更改不会对图像产生任何影响,并且每次更改的输出都保持不变。
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
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", 400);
cropIntent.putExtra("outputY", 400);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
输出 x 和 y 值的变化不会影响被裁剪的图像大小。图像大小始终保持 160*160。只是当我改变纵横比时,图像尺寸会发生一点变化。但是我需要为 x 和 y
提供一个特定的大小如果有任何关于图像裁剪的库并且在所有设备上都支持,那就太棒了
设置 aspectX 和 aspectY 与 outputX 和 outputY 的比率。
假设您希望结果图像为 200 * 300 像素。然后,只需将 aspectX 设置为 2,将 aspectY 设置为 3。您将获得所需的图像尺寸。这是更新后的代码。
private void performCrop(String picUri) {
try {
//Start Crop Activity
Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
File f = new File(picUri);
Uri contentUri = Uri.fromFile(f);
cropIntent.setDataAndType(contentUri, "image/*");
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 2);
cropIntent.putExtra("aspectY", 3);
// indicate output X and Y
cropIntent.putExtra("outputX", 200);
cropIntent.putExtra("outputY", 300);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, RESULT_CROP);
}
// respond to users whose devices do not support the crop action
catch (ActivityNotFoundException anfe) {
// display an error message
String errorMessage = "your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}