【问题标题】:How to add Crop Feature to Camera Intent or Gallery Intent & in Web-view?or In JavaScript如何在 Web 视图或 JavaScript 中将裁剪功能添加到相机意图或画廊意图
【发布时间】:2016-02-23 05:24:04
【问题描述】:

我跟着this 捕获或从 Web 视图中选择文件并上传...这是完美的,适用于所有 android 版本..

所以我想在那里添加裁剪意图...在相机捕获/图库后裁剪然后上传所有这些从 Webview 发生

我收到 this 意图添加裁剪图像。我想在 MainActivity 中添加它。在捕获表单相机和图库中..

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

所以我想裁剪和上传的可能是相机或画廊..

任何人都可以建议我如何将作物意图添加到主要活动..

更新 1

我有捕捉相机和查看画廊的意图。以类似的方式,我可以选择裁剪意图……但我想将此裁剪应用于相机意图和画廊,但所有这些都需要在 webview 中发生(主要活动)...

请在回答之前检查我的Mainactivity...。

我想从相机意图和画廊意图添加裁剪意图..它应该能够上传......最小分辨率......不超过 2 兆像素..如果小于也没有问题......就像@987654324 @位图...

在更新中我再次添加了相同的链接不要混淆......

这里都需要在webview中裁剪上传...

更新 2

是否可以在我的 MainActivity 中使用 this 库...如果从网络视图裁剪相机捕获并在同一网络视图中上传...

【问题讨论】:

  • 您遇到了什么问题?上面的代码不行吗?
  • 先生..我想在主要活动中添加 CROP 意图,您能建议我在哪里添加.. 如果可能,添加和更新答案...
  • 这意味着您可以裁剪图像但无法将其设置为 webview,对吧?
  • 不,先生..我可以在单独的应用程序中裁剪..但不能在 webview mainactivity 中......有意图
  • Android does not have a CROP Intent。有很多image cropping libraries for Android。请使用一个。或者,在您的情况下,找到一些基于 JavaScript 的图像裁剪库,并从您的 Web 内容中使用它。

标签: android android-intent webview android-camera crop


【解决方案1】:

首先,在你的gradle文件中添加依赖:

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

然后,下面是裁剪图像的逻辑。将此方法放在您的 Activity 类中并从 Activity 调用此方法可能会根据您的要求在某些按钮上单击,并传递图像的 URI。

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

裁剪图片后会在Activity的onActivityResult中得到结果

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

之后

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

我希望这对你有用。

【讨论】:

  • 先生,您能告诉我在我的主要活动中在哪里添加所有这些。请帮助...用我的主要活动更新您的答案..
  • 请粘贴您的 MainActivity.java。我将对您的文件进行相应的更改并将其粘贴到答案部分
  • 它与this 相同,我也遵循相同的...请按原样更新..
【解决方案2】:

您可以通过简单的方式做到这一点:

首先使用intent to camera捕获图像,

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

在 onActivityResult() 中,您可以捕获结果,该结果将返回捕获的图像的 url。

然后您可以意图裁剪图像:

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

您将再次在onActivityResult() 中获取裁剪后的图像。

CropImageIntentBuilder 源代码在 github 上。请在下面找到链接

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

更新:

您可以在某些操作(例如单击按钮)上触发相机的意图,我希望您将按钮放在 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;
    }

我希望它现在对你更有意义。

【讨论】:

  • 我正在编辑我的答案以使其对您更有意义。
猜你喜欢
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 2015-02-02
  • 1970-01-01
  • 2012-01-25
  • 2013-04-29
  • 2011-07-10
相关资源
最近更新 更多