简体   繁体   English

在不同设备上裁剪图像

[英]Crop image on different devices

I want to take a picture with the camera and crop it. 我想用相机拍照并裁剪。 This works great (with the second code) on newer devices with this code I found on the community wiki: 我在社区Wiki上找到了以下代码,这在更新的设备上效果很好(使用第二个代码):

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.gallery", "com.android.camera.CropImage");

On some Android versions, including the newest, com.android.gallery doesn't exist anymore. 在某些Android版本(包括最新版本)上,com.android.gallery不再存在。 You need to use this then: 您需要使用以下代码:

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.google.android.gallery3d", "com.android.gallery3d.app.CropImage");

Of course I want to support older devices too. 当然,我也想支持较旧的设备。 What is meant with "some Android versions"? “某些Android版本”是什么意思? Can someone give me an API level? 有人可以给我API级别吗? Or are there any final constances in the Android source which I can use to select the correct Strings for the intent? 还是Android源代码中有最后的常量可以用来为意图选择正确的字符串?

Some devices don't support cropping, meaning that their gallery application does not have it built it. 有些设备不支持裁剪,这意味着其图库应用程序没有构建它。 The best solution is building a cropping mechanism into your app. 最好的解决方案是在您的应用程序中构建裁剪机制。 Here is a good open source cropper: 这是一个很好的开源裁剪器:

https://github.com/edmodo/cropper https://github.com/edmodo/cropper

I found a better code for this problem. 我为这个问题找到了更好的代码。 This here will search for apps which are able to crop images and start the first that is found. 这将在此处搜索能够裁剪图像的应用程序,并启动找到的第一个应用程序。 Hope that help someone. 希望对某人有所帮助。

Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");

List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();

if (size == 0) 
{           
    Toast.makeText(context, "Can not find image crop app", Toast.LENGTH_SHORT).show();      
    return null;
} 
else 
{
    ResolveInfo res = list.get(0);

    Intent intent = new Intent();
    intent.setClassName(res.activityInfo.packageName, res.activityInfo.name);

    intent.setData(imageCaptureUri);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    startActivityForResult(intent, CROP_FROM_CAMERA);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM