简体   繁体   中英

How to add Crop Feature to Camera Intent or Gallery Intent & in Web-view?or In JavaScript

I followed this to Capture or Choose File from Web-view and Upload...This is perfect and working for all android versions..

So over there I want to Add Crop Intent... To crop After Camera Capturing/Gallery then Upload all this Happen From Webview

I got this intent to add for Crop Image.. I want to add this in MainActivity.. In Both Capture form Camera and Gallery ..

Intent cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
cropIntent.setDataAndType(data.getData(), "image/*");
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
cropIntent.putExtra("outputFormat",
        Bitmap.CompressFormat.JPEG.toString());
// set crop properties
cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
// retrieve data on return
cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in
// onActivityResult
startActivityForResult(cropIntent, 3);

So It may Be camera or Gallery I want to Crop and Upload..

Can Any one suggest me How to add Crop Intent to Main activity..

Update 1

I Have a Intent to capture camera And View gallery.. In the similar way I have option for Crop Intent... But I want to apply this crop for Both camera intent and gallery but all these need to happen in webview(Mainactivity)...

Please Check my Mainactivity ... Before answering..

I want to add Crop Intent fro camera intent and Gallery intent.. and it should able to upload... with min resolution... not more than 2megapixel.. if less than also no problem... like this at bitmap...

In update I have again added same links don't get confused...

All here It needs to crop and upload in webview...

Update 2

Is it possible use this Library in my MainActivity... In case of camera capture from web-view crop and upload in same webview...

First of all, add the dependency in your gradle file:

compile 'com.soundcloud.android:android-crop:1.0.1@aar'

Then, below is the logic to crop the image. Put this method in your Activity class and call this method from Activity may be on some button click depending on your requirements, passing the URI of the image.

private void beginCrop(Uri source) {
        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
        Crop.of(source, destination).asSquare().start(this);
    } 

After cropping the image you'll get the result in onActivityResult of the Activity

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
         if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, result);
        } 
    } 

after that

private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            ImageView.setImageURI(Crop.getOutput(result));
        } else if (resultCode == Crop.RESULT_ERROR) {
            Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
        } 

I hope this works for you.

You can do it in simple way:

First capture the image using intent to camera,

 Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intentCamera, Constants.CAMERA_REQUEST_CODE);

In onActivityResult() you can capture the result whihc will return the url of the image captured.

Then you can intent for croping the image:

private void callCrop(Uri sourceImage) {
        CropImageIntentBuilder cropImage = new CropImageIntentBuilder(200, 200, getURL());
        cropImage.setOutlineColor(Color.WHITE);
        cropImage.setSourceImage(sourceImage);
        cropImage.setDoFaceDetection(false);
        startActivityForResult(cropImage.getIntent(this), Constants.CROP_REQUEST_CODE);
    }

You will get the croped image in again onActivityResult().

CropImageIntentBuilder source code is on github. Please find the link below

https://github.com/lvillani/android-cropimage/blob/master/CropImage/src/main/java/com/android/camera/CropImageIntentBuilder.java

UPDATE :

You can fire that intent of camera on some action like on click of button and I hope you will put you button in Activity.

camerBtn.setOnClickListener(new OnClickListener(){
  @Override
    public void onClick(View v) {
      Intent intentCamera = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intentCamera, Constants.CAMERA_REQUEST_CODE);
    }
});


@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap saveBitmap = null;
        if (resultCode == RESULT_OK) {
            if (requestCode == Constants.CAMERA_REQUEST_CODE) {

                if (data != null) {

                    Uri currentImageUri = data.getData();

                    if (currentImageUri != null) {
                        Bitmap currentBitmap = uriToBitmap(currentImageUri);
                        Bitmap rotatedBitmap = rotateImage(currentBitmap, 90); // Rotate bitmap by 90' to avoid the orientation change of image.
                        saveImageToFile(rotatedBitmap);  // save bitmap with rotation of 90' .
                        callCrop(getURL());
                    }

                } else {
                    return;
                }           
            } else if (requestCode == Constants.CROP_REQUEST_CODE) {
                saveBitmap = BitmapFactory.decodeFile(getFile().getAbsolutePath());
                String convertedImage = Utils.bitMapToString(saveBitmap);                
            }        
        super.onActivityResult(requestCode, resultCode, data);
    }



/**
     * To get Bitmap from respective Uri.
     *
     * @param selectedFileUri
     * @return bitmap
     */
    private Bitmap uriToBitmap(Uri selectedFileUri) {
        Bitmap image = null;
        try {
            ParcelFileDescriptor parcelFileDescriptor =
                    getContentResolver().openFileDescriptor(selectedFileUri, "r");
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            image = BitmapFactory.decodeFileDescriptor(fileDescriptor);


            parcelFileDescriptor.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return image;
    }

    /**
     * To rotate bitmap by an given angle(in degree).
     *
     * @param img    bitmap which you want to rotate.
     * @param degree
     * @return rotated bitmap.
     */
    private static Bitmap rotateImage(Bitmap img, int degree) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degree);
        Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
        img.recycle();
        return rotatedImg;
    }

I hope it will make more sense to you now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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