簡體   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