how to zoom + crop a image and display the croped image on imageview

前端 未结 2 1752
陌清茗
陌清茗 2020-11-30 13:35

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 zo

相关标签:
2条回答
  • 2020-11-30 13:55

    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;
        }
    }
    
    0 讨论(0)
  • 2020-11-30 14:05

    I don't know whether this will solve your problem . I created somewhat similar thing for my project adding the functionality to pick image from Camera or Gallery, then showing a fixed cropping window with image zoomin out functionality. Combining Photoview and Cropper tool. Shared the code on Github. You can modify the code to fit in your need. Added an apk file in the project. Use real device for testing camera as emulator doesn't handle camera well. Here's the link to my project.

    https://github.com/ozeetee/AndroidImageZoomCrop

    0 讨论(0)
提交回复
热议问题