gpt4 book ai didi

android - 通过在 Android 中传递图像文件路径来裁剪图像

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:51:06 25 4
gpt4 key购买 nike

我试过下面的代码。但是,它总是生成 160*160 维图像。

try {   
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(Uri.fromFile(pictureFile), "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 100);
cropIntent.putExtra("aspectY", 100);
cropIntent.putExtra("scale", true);

//indicate output X and Y
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);

} 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();

}

我想通过传递路径来裁剪图像。我不想从默认相机应用程序或图库中捕捉/选择。请帮我解决这个问题。

最佳答案

我已经解决了这个问题,方法是在调用 Intent 之前创建一个新文件并传递此文件路径以通过 Intent 存储裁剪后的图像。这是解决方案。

private Uri mCropImagedUri;
private final int CROP_IMAGE = 100;//unique request code number. Which is used to identify the request result in onActivityResult()
/**Crop the image
* @return returns <tt>true</tt> if crop supports by the device,otherwise false*/
private boolean performCropImage(){
try {
if(mFinalImageUri!=null){
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mFinalImageUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
//retrieve data on return
cropIntent.putExtra("return-data", false);

File f = createNewFile("CROP_");
try {
f.createNewFile();
} catch (IOException ex) {
VLLog.e("io", ex.getMessage());
}

mCropImagedUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
return true;
}
}
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();
return false;
}
return false;
}

private File createNewFile(String prefix){
if(prefix==null || "".equalsIgnoreCase(prefix)){
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
if(!newDirectory.exists()){
if(newDirectory.mkdir()){
VLLog.d(mContext.getClass().getName(), newDirectory.getAbsolutePath()+" directory created");
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if(file.exists()){
//this wont be executed
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}

return file;
}

所以在这里我们不应该关心 onActivityResult() 方法中的数据。

这里是关于裁剪图像的完整信息。我用这个来解决。 http://www.androidworks.com/crop_large_photos_with_android

关于android - 通过在 Android 中传递图像文件路径来裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18463102/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com