简体   繁体   English

如何将Crop Feature添加到Camera Intent或Gallery Intent&在Web-view?或JavaScript中

[英]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.. 我按照这个从Web视图捕获或选择文件和上传...这是完美的,适用于所有Android版本..

So over there I want to Add Crop Intent... To crop After Camera Capturing/Gallery then Upload all this Happen From Webview 所以在那里我想添加裁剪意图...裁剪相机捕获/图库然后从Webview上传所有这些Happen

I got this intent to add for Crop Image.. I want to add this in MainActivity.. In Both Capture form Camera and Gallery .. 我有这个意图为Crop Image添加..我想在MainActivity中添加它..在Capture中形成Camera和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.. 可以任何人建议我如何将Crop Intent添加到主要活动..

Update 1 更新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)... 我有一个意图捕获相机和视图库..以类似的方式我有作物意图的选项...但我想将这个作物应用于相机意图和画廊,但所有这些都需要在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... 我想为相机意图和画廊意图添加Crop Intent ..它应该能够上传...具有最小分辨率...不超过2megapixel ..如果不到也没问题...就像这样在位图.. 。

In update I have again added same links don't get confused... 在更新中我再次添加相同的链接不要混淆...

All here It needs to crop and upload in webview... 所有这里需要在webview中裁剪和上传...

Update 2 更新2

Is it possible use this Library in my MainActivity... In case of camera capture from web-view crop and upload in same webview... 是否可以在我的MainActivity中使用库...如果从Web视图裁剪捕获相机并在同一webview中上传...

First of all, add the dependency in your gradle file: 首先,在gradle文件中添加依赖项:

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. 将此方法放在Activity类中,并根据您的要求在Activity上调用此方法,并传递图像的URI。

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 裁剪图像后,您将获得活动的onActivityResult中的结果

@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. 在onActivityResult()中,您可以捕获结果,该结果将返回捕获的图像的URL。

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(). 您将再次获得onActivityResult()中的图像。

CropImageIntentBuilder source code is on github. CropImageIntentBuilder源代码在github上。 Please find the link below 请在下面找到链接

https://github.com/lvillani/android-cropimage/blob/master/CropImage/src/main/java/com/android/camera/CropImageIntentBuilder.java 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. 我希望现在对你更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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