繁体   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

我按照这个从Web视图捕获或选择文件和上传...这是完美的,适用于所有Android版本..

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

我有这个意图为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);

所以它可能是相机或图库我想裁剪和上传..

可以任何人建议我如何将Crop Intent添加到主要活动..

更新1

我有一个意图捕获相机和视图库..以类似的方式我有作物意图的选项...但我想将这个作物应用于相机意图和画廊,但所有这些都需要在webview(Mainactivity)中发生。 ..

请检查我的主动 ...在回答之前..

我想为相机意图和画廊意图添加Crop Intent ..它应该能够上传...具有最小分辨率...不超过2megapixel ..如果不到也没问题...就像这样在位图.. 。

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

所有这里需要在webview中裁剪和上传...

更新2

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

首先,在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);
    } 

裁剪图像后,您将获得活动的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();
        } 

我希望这适合你。

你可以用简单的方式做到:

首先使用意图相机拍摄图像,

 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

更新:

您可以通过单击按钮来触发相机的某些操作,我希望您将按钮放在“活动”中。

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

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

暂无
暂无

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

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