我很难弄清楚如何让用户裁剪图片。
我想给位图变量加载位图来裁剪图片,然后再将其设置为墙纸。但是我没有这样做...这是我尝试过的。

第一个版本。 =按预期工作,但返回的图像分辨率较差。将输出更改为更高的值无济于事。正如我在某些帖子中看到的那样,不建议您使用相机,因为并非所有设备都支持此功能。

Intent intent = new Intent("com.android.camera.action.CROP");
String path = Images.Media.insertImage(context.getContentResolver(), loaded,null, null);
Uri uri = Uri.parse(path);
intent.setData(uri);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 300);
intent.putExtra("outputY", 300);
intent.putExtra("noFaceDetection", true);
intent.putExtra("return-data", true);
startActivityForResult(intent, 2);

第二。加载图像选择器,然后裁剪。如何配置它以直接在图像上加载裁切?就像版本1中一样
Intent photoPickerIntent = new Intent(MediaStore.ACTION_PICK);
photoPickerIntent.setData(uri);
photoPickerIntent.putExtra("crop", "true");
photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
photoPickerIntent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
startActivityForResult(photoPickerIntent, 2);

和onActivity结果
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_OK) { return; }
    if(requestCode == 2) {
        Bundle extras = data.getExtras();
        if(extras != null ) {
            Bitmap photo = extras.getParcelable("data");
            loaded = photo;
        }
        WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());

        try {
            myWallpaperManager.setBitmap(loaded);
        } catch (IOException e) {}
    }
}

我不知道这些是完成此操作的正确方法,但我希望有人可以指出正确的方向。哪个,为什么以及如何使用。

更新:我仍在等待某人指出如何正确执行操作,以下答案有效,但返回的图像分辨率较差,因此不能使用它们

最佳答案

好的,亲爱的,在这里,我将裁剪图像的整个代码放在了Android中。
这是全局变量。

    //This For Image Crop
        /**
         *  Uri for set image crop option .
         */
        private Uri mImageCaptureUri;
        /**
         *  int for set key and get key from result activity .
         */
        public final int CROP_FROM_CAMERA = 0;

/**
     * Bitmap for apply Crop Operation Result.
     */
    private Bitmap _tempOpration;

    //This is Crop Method.

/**
     * Method for apply Crop .
     * @param filePath -  String path of file .
     */
    private void doCrop(String filePath){
        try{
            //New Flow
            mImageCaptureUri = Uri.fromFile(new File(filePath));

            final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setType("image/*");
            List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );

            int size = list.size();
            if (size == 0)
            {
                Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
                return;
            }
            else
            {
                intent.setData(mImageCaptureUri);
                intent.putExtra("outputX", 300);
                intent.putExtra("outputY", 300);
                intent.putExtra("aspectX", 1);
                intent.putExtra("aspectY", 1);
                intent.putExtra("scale", true);
                intent.putExtra("return-data", true);

                if (size == 1)
                {
                    Intent i = new Intent(intent);
                    ResolveInfo res = list.get(0);
                    i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
                    startActivityForResult(i, CROP_FROM_CAMERA);
                }

                else
                {
                    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 )
                        {
                            startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                        }
                    });

                    builder.setOnCancelListener( new DialogInterface.OnCancelListener()
                    {
                        public void onCancel( DialogInterface dialog )
                        {
                            if (mImageCaptureUri != null )
                            {
                                getContentResolver().delete(mImageCaptureUri, null, null );
                                mImageCaptureUri = null;
                            }
                        }
                    } );
                    AlertDialog alert = builder.create();
                    alert.show();
                }
            }
        }
        catch (Exception ex)
        {
            genHelper.showErrorLog("Error in Crop Function-->"+ex.toString());
        }
    }

这是在应用程序中查找裁剪 Activity Intent 的另一个类女巫。

CropOption类别。
public class CropOption
{
    public CharSequence title;
    public Drawable icon;
    public Intent appIntent;
}

这是用于显示列表。

CropOptionAdapter
public class CropOptionAdapter extends ArrayAdapter<CropOption>
{
    private ArrayList<CropOption> mOptions;
    private LayoutInflater mInflater;

    public CropOptionAdapter(Context context, ArrayList<CropOption> options)
    {
        super(context, R.layout.crop_selector, options);

        mOptions    = options;

        mInflater   = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup group)
    {
        if (convertView == null)
            convertView = mInflater.inflate(R.layout.crop_selector, null);

        CropOption item = mOptions.get(position);

        if (item != null) {
            ((ImageView) convertView.findViewById(R.id.iv_icon)).setImageDrawable(item.icon);
            ((TextView) convertView.findViewById(R.id.tv_name)).setText(item.title);

            return convertView;
        }

        return null;
    }
}

CropOptionAdapter的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="center_vertical">

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""/>
</LinearLayout>

这是resultActivity.witch提供裁剪图像的结果。
/**
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) return;
        switch (requestCode)
        {
        case CROP_FROM_CAMERA:
            if (data == null)
            {
                genHelper.showToast("No Crop Activity in This");
                return;
            }
            final Bundle extras = data.getExtras();
            if (extras != null)
            {
                try
                {

                    _tempOpration=extras.getParcelable("data");
                    imageLayout.setImageBitmap(_tempOpration);
                    _tempOpration=null;

                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
            break;
        }

    }

//执行此类型在我的实时应用程序上有效。

genHelper.showToast(“此 Activity 没有裁剪”);

是我的一般类(class),以帮助显示 toast 消息和错误日志。

祝你好运。

关于android - 让用户裁剪图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15115498/

10-12 00:11
查看更多