如何放大+裁剪图像,并显示在ImageView的的croped图像图像、ImageView、croped

2023-09-12 22:05:51 作者:惆怅旅客

我在做Android应用程序中,我有我选择一个图像从画廊的情景,裁剪,并显示在的ImageView 。现在,在种植的时候,我想放大的图像,以及。我使用 TouchImageView 类缩放图像。

请注意,如果我只想要 TouchImageView 适用于的ImageView 它工作正常。但是,当我用它裁剪功能,这是行不通的。

我应该如何申请作物+缩放功能在时间上的的ImageView ? 任何形式的帮助将AP preciated。下面是我的尝试。

 保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,因此code,数据);
        的System.out.println(要求code);
         如果(结果code!= RESULT_OK){

                返回;
            }

            点阵位图;

            开关(要求code){

                情况下0:

                     mImageCaptureUri = data.getData();
                    doCrop();

                    打破;
                情况1:

                    mImageCaptureUri = data.getData();
                    doCrop();
                    打破;
                案例2:
                    捆绑额外= data.getExtras();
                    / **
                     *裁剪图像后,得到了裁剪图像的位图和
                     *在ImageView的显示。
                     * /
                    如果(临时演员!= NULL){
                          位图照片= extras.getParcelable(数据);
                          img_v.setImageBitmap(照片);

                          // MYBITMAP = getCircleImage(照片);

                          //串image_base64 = postimage();

                         //新PostCoverTask()执行(用户ID,image_base64)。
                    }

                    文件F =新的文件(mImageCaptureUri.getPath());
                    / **
                     *删除临时映像
                     * /
                    如果(f.exists())
                          f.delete();
                    打破;
            }
            super.onActivityResult(要求code,因此code,数据);


    }

    私人无效doCrop()
    {
        最后的ArrayList< CropOption> cropOptions =新的ArrayList< CropOption>();
        / **
         *通过启动一个意图打开图像裁剪的应用程序
         *'com.android.camera.action.CROP。
         * /
        意向意图=新的意图(com.android.camera.action.CROP);
        intent.setDataAndType(mImageCaptureUri,图像/ *);

        / **
         *检查是否有安装图像栽跟头的应用程序。
         * /
        名单< ResolveInfo>表= getPackageManager()。queryIntentActivities(
                意图,0);

        INT大小=则为list.size();


        / **
         *如果没有图像栽跟头的应用程序,显示警告信息
         * /
        如果(大小== 0)
        {
            Toast.makeText(这一点,找不到图像裁剪的应用程序,
                    Toast.LENGTH_SHORT).show();

            返回;
        }
        其他
        {
            大小= 1;
            / **
             *指定图像路径,裁剪尺寸和比例
             * /
            // intent.setData(mImageCaptureUri);
            intent.putExtra(裁剪,真);
            intent.putExtra(outputX,256);
            intent.putExtra(outputY,256);
            intent.putExtra(aspectX,3);
            intent.putExtra(aspectY,1);
            intent.putExtra(规模化,真正的);
            intent.putExtra(scaleUpIfNeeded,真正的);
            intent.putExtra(回归数据,真正的);
            的System.out.println(把形象外);
            / **
             *有posibility当多个图像割草机的应用程序存在,
             *所以我们必须首先检查它。如果只有一个应用程序,开
             *然后应用程序。
             * /

            如果(尺寸== 1){
                意图I =新的意向(意向);
                ResolveInfo解析度= NULL;
                为(中间体的i1 = 0; i1的&其中;则为list.size(); I1 +)
                {
                    如果(list.get(I1)!= NULL)
                    {
                        RES = list.get(I1);
                        打破;
                    }
                }
                i.setComponent(新的组件名(res.activityInfo.packageName,
                        res.activityInfo.name));
                的System.out.println(大小等于+尺寸);
                startActivityForResult(1,2);
            }
            其他
            {
                的System.out.println(大小等于+尺寸);
                / **
                 *如果有多个应用程序存在,创建一个自定义选择器
                 *让用户选择的应用程序。
                 * /
                对于(ResolveInfo RES:名单)
                {
                    最后CropOption合作=新CropOption();

                    co.title = getPackageManager()。getApplicationLabel(
                            res.activityInfo.applicationInfo);
                    co.icon = getPackageManager()。getApplicationIcon(
                            res.activityInfo.applicationInfo);
                    co.appIntent =新的意图(意图);

                    co.appIntent
                    .setComponent(新的组件名(
                            res.activityInfo.packageName,
                            res.activityInfo.name));

                    cropOptions.add(共);
                }

                CropOptionAdapter适配器=新CropOptionAdapter(
                        getApplicationContext(),cropOptions);

                AlertDialog.Builder建设者=新AlertDialog.Builder(本);
                builder.setTitle(选择作物应用程序);
                builder.setAdapter(适配器,
                        新DialogInterface.OnClickListener()
                {


                    公共无效的onClick(DialogInterface对话,诠释项){
                        的System.out.println(选项+ cropOptions.get(项目));
                        //startActivityForResult(cropOptions.get(item).appIntent,
                            // CROP_FROM_CAMERA);
                        的System.out.println(builder.setAdapter);
                    }
                });

                builder.setOnCancelListener(新DialogInterface.OnCancelListener(){
                    @覆盖
                    公共无效OnCancel的(DialogInterface对话){

                        如果(mImageCaptureUri!= NULL){
                            getContentResolver()。删除(mImageCaptureUri,空,
                                    空值);
                            mImageCaptureUri = NULL;

                        }
                        的System.out.println(点击取消);
                    }
                });

                AlertDialog警报= builder.create();

                alert.show();
                的System.out.println(警告);
            }
        }
    }
 

解决方案

我使用 cropimage 从库Github上。这也符合您的要求。这是我在我的项目中使用这个库。 将此行添加到您的清单文件:

 <活动机器人:名称=eu.janmuller.android.simplecropimage.CropImage/>
 
制作PPT,怎样裁剪图片

从画廊或相机,并调用这个函数选择图片:

 公共无效runCropImage(字符串路径){
    意向意图=新的意图(这一点,CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH,路径);
    intent.putExtra(CropImage.SCALE,真正的);
    intent.putExtra(CropImage.ASPECT_X,2); //变化比这里通过意向
    intent.putExtra(CropImage.ASPECT_Y,2);
    startActivityForResult(意向,REQUEST_ code_CROP_IMAGE); //最后的静态INT 1
}
 

而在你的onActivityResult:

  @覆盖
保护无效onActivityResult(INT申请code,INT结果code,意图数据){
    // TODO自动生成方法存根
    super.onActivityResult(要求code,因此code,数据);
    开关(要求code){
    //画廊和相机ommitted
    案例REQUEST_ code_CROP_IMAGE:
        字符串路径= data.getStringExtra(CropImage.IMAGE_PATH);
        //如果没有得到任何
        如果(路径== NULL){
            返回;
        }
        //裁剪位图
         点阵位图= BitmapFactory.de codeFILE(路径);
        imageView.setImageBitmap(位);
        打破;
    默认:
        打破;
    }
}
 

I am making android app in which I have a scenario that I pick an image from gallery, crop it and show it on imageview. Now at the time of cropping I want to zoom that image as well. I am using TouchImageView class for zooming an image.

Note that if I only want to apply TouchImageView on ImageView it works fine. But when I used it with crop functionality it doesn't work.

How should I apply crop+zoom functionality at a time on ImageView? Any type of help will be appreciated. Below is my try.

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

解决方案

I am using cropimage library from Github. It fits your requirement well. This is how I use this library in my project. Add this line to your manifest file:

 <activity android:name="eu.janmuller.android.simplecropimage.CropImage" />

Select image from gallery or camera and call this function:

public void runCropImage(String path) {
    Intent intent = new Intent(this, CropImage.class);
    intent.putExtra(CropImage.IMAGE_PATH, path);
    intent.putExtra(CropImage.SCALE, true);
    intent.putExtra(CropImage.ASPECT_X, 2);//change ration here via intent
    intent.putExtra(CropImage.ASPECT_Y, 2);
    startActivityForResult(intent, REQUEST_CODE_CROP_IMAGE);//final static int 1
}

And in your onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
    // gallery and camera ommitted
    case REQUEST_CODE_CROP_IMAGE:
        String path = data.getStringExtra(CropImage.IMAGE_PATH);
        // if nothing received
        if (path == null) {
            return;
        }
        // cropped bitmap
         Bitmap bitmap = BitmapFactory.decodeFile(path);
        imageView.setImageBitmap(bitmap);
        break;
    default:
        break;
    }
}