在android中裁剪图像

I Y*_*u C 33 android image android-crop

我想裁剪图像我发现了一些相当有用的但是不知何故就像缺少了未选区域的变暗所以我想知道有谁知道怎么样?还是引导我走向正确的方向?我发现的在线教程显示会使所选区域变暗,但是当我使用它时,它不会.请帮助我多多感谢,抱歉我的英语不好.

我使用的教程的链接.

裁剪图像教程1

裁剪图像教程2

我希望它是这样的.

我希望它是这样的

editButton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent goEdit;
            goEdit = new Intent(PreviewActivity.this, CropImage.class);
            goEdit.putExtra("image-path", path);
            goEdit.putExtra("scale", true);
            goEdit.putExtra("fileName", nameFromPath);
            //finish();
            checkEdit = true;
            startActivityForResult(goEdit,0);

        }
});
Run Code Online (Sandbox Code Playgroud)

编辑 我使用此按钮侦听器通过调用类CropImage活动调用cropImage文件.这是一个自定义的意图,而不是Android内部的裁剪功能,但我认为是它的副本,以便它支持所有版本,但当我调用它时,所选区域不亮,我不知道问题可以在哪里引导我?谢谢这是我正在使用drioid4you裁剪图像的库

Akb*_*ali 51

你能使用默认的android Crop功能吗?

这是我的代码

private void performCrop(Uri picUri) {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        // set crop properties here
        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", 128);
        cropIntent.putExtra("outputY", 128);
        // retrieve data on return
        cropIntent.putExtra("return-data", true);
        // start the activity - we handle returning in onActivityResult
        startActivityForResult(cropIntent, PIC_CROP);
    }
    // respond to users whose devices do not support the crop action
    catch (ActivityNotFoundException anfe) {
        // display an error message
        String errorMessage = "Whoops - your device doesn't support the crop action!";
        Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
        toast.show();
    }
}
Run Code Online (Sandbox Code Playgroud)

宣布:

final int PIC_CROP = 1;
Run Code Online (Sandbox Code Playgroud)

在顶部.

在onActivity结果方法中,编写以下代码:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PIC_CROP) {
        if (data != null) {
            // get the returned data
            Bundle extras = data.getExtras();
            // get the cropped bitmap
            Bitmap selectedBitmap = extras.getParcelable("data");

            imgView.setImageBitmap(selectedBitmap);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我很容易实现,也显示变暗的区域.

  • [Android没有`CROP``Intent`](https://commonsware.com/blog/2013/01/23/no-android-does-not-have-crop-intent.html). (20认同)
  • 在我的情况下,`data.getExtras()`返回`null`,那么我如何获得`Bitmap`? (3认同)

Soo*_*tos 8

这个库:Android-Image-Cropper对 CropImages 非常强大。目前在 github 上有 3,731 颗星。

您将使用几行代码裁剪图像。

1 - 将依赖项添加到 buid.gradle(模块:app)

compile 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
Run Code Online (Sandbox Code Playgroud)

2 - 将权限添加到 AndroidManifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)

3 - 将 CropImageActivity 添加到 AndroidManifest.xml

<activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
 android:theme="@style/Base.Theme.AppCompat"/>
Run Code Online (Sandbox Code Playgroud)

4 - 根据您的要求,从以下情况之一开始活动。

// start picker to get image for cropping and then use the image in cropping activity
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.start(this);

// start cropping activity for pre-acquired image saved on the device
CropImage.activity(imageUri)
.start(this);

// for fragment (DO NOT use `getActivity()`)
CropImage.activity()
.start(getContext(), this);
Run Code Online (Sandbox Code Playgroud)

5 - 在 onActivityResult 中获取结果

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
    CropImage.ActivityResult result = CropImage.getActivityResult(data);
    if (resultCode == RESULT_OK) {
      Uri resultUri = result.getUri();
    } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
      Exception error = result.getError();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

您可以进行多种自定义,例如将纵横比或形状设置为 RECTANGLE、OVAL 等等。