在android中裁剪图像

新手上路,请多包涵

我想对图像进行裁剪,我发现了一些非常有用的图像,但不知何故就像没有使未选择的区域变暗,所以我想知道有人知道怎么做吗?或引导我走向正确的方向?我发现的在线教程显示,它会使所选区域变暗,但是当我使用它时,它不会。请帮助我,非常感谢,并为我的英语不好感到抱歉。

我使用的教程的链接。

裁剪图像教程 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);

        }
});

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

原文由 I Yeu C 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 846
2 个回答

您可以使用默认的 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();
    }
}

宣布:

 final int PIC_CROP = 1;

在顶部。

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

这对我来说很容易实现,并且还显示了较暗的区域。

原文由 Akbari Dipali 发布,翻译遵循 CC BY-SA 3.0 许可协议

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

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

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

 compile 'com.theartofdev.edmodo:android-image-cropper:2.7.+'

2 - 将权限添加到 AndroidManifest.xml

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

3 - 将 CropImageActivity 添加到 AndroidManifest.xml

 <activity android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
 android:theme="@style/Base.Theme.AppCompat"/>

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

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

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

原文由 Soon Santos 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题