【发布时间】:2014-06-05 10:36:56
【问题描述】:
我正在制作 android 应用程序,其中我有一个场景,我从画廊中挑选一张图片,裁剪它并在imageview 上显示它。现在在裁剪时我也想缩放该图像。我正在使用TouchImageView 类来缩放图像。
请注意,如果我只想在ImageView 上应用TouchImageView,它可以正常工作。但是当我将它与裁剪功能一起使用时,它就不起作用了。
我应该如何在ImageView 上一次应用裁剪+缩放功能?
任何类型的帮助将不胜感激。下面是我的尝试。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(requestCode);
if (resultCode != RESULT_OK) {
return;
}
Bitmap bitmap;
switch (requestCode) {
case 0:
mImageCaptureUri = data.getData();
doCrop();
break;
case 1:
mImageCaptureUri = data.getData();
doCrop();
break;
case 2:
Bundle extras = data.getExtras();
/**
* After cropping the image, get the bitmap of the cropped image and
* display it on imageview.
*/
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
img_v.setImageBitmap(photo);
//myBitmap = getCircleImage(photo);
//String image_base64 = postimage();
// new PostCoverTask().execute(userid, image_base64);
}
File f = new File(mImageCaptureUri.getPath());
/**
* Delete the temporary image
*/
if (f.exists())
f.delete();
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void doCrop()
{
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
/**
* Open image crop app by starting an intent
* ‘com.android.camera.action.CROP‘.
*/
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mImageCaptureUri,"image/*");
/**
* Check if there is image cropper app installed.
*/
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
/**
* If there is no image cropper app, display warning message
*/
if (size == 0)
{
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
}
else
{
size=1;
/**
* Specify the image path, crop dimension and scale
*/
// intent.setData(mImageCaptureUri);
intent.putExtra("crop", "true");
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX",3);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
System.out.println("Put image in Extra");
/**
* There is posibility when more than one image cropper app exist,
* so we have to check for it first. If there is only one app, open
* then app.
*/
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res=null ;
for (int i1=0;i1<list.size();i1++)
{
if(list.get(i1)!=null)
{
res = list.get(i1);
break;
}
}
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
System.out.println("size is equal to "+size);
startActivityForResult(i, 2);
}
else
{
System.out.println("size is equal to "+size);
/**
* If there are several app exist, create a custom chooser to
* let user selects the app.
*/
for (ResolveInfo res : list)
{
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(
res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(
res.activityInfo.applicationInfo);
co.appIntent = new Intent(intent);
co.appIntent
.setComponent(new ComponentName(
res.activityInfo.packageName,
res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(
getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item) {
System.out.println("option "+cropOptions.get(item));
//startActivityForResult(cropOptions.get(item).appIntent,
// CROP_FROM_CAMERA);
System.out.println("builder.setAdapter");
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mImageCaptureUri != null) {
getContentResolver().delete(mImageCaptureUri, null,
null);
mImageCaptureUri = null;
}
System.out.println("Click on cancel");
}
});
AlertDialog alert = builder.create();
alert.show();
System.out.println("alert");
}
}
}
【问题讨论】:
-
您能否具体说明“不起作用”是什么意思,可能是错误消息?
-
不,我希望上面的代码是正确的。但它只为我裁剪图像.. 我希望在裁剪过程中我放大和缩小图像并在缩放后裁剪图像的特定部分。