【问题标题】:Crop image in android在android中裁剪图像
【发布时间】:2013-03-05 16:17:44
【问题描述】:

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

我使用的教程的链接。

Crop image tutorial 1

Crop Image tutorial 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 crop image

【问题讨论】:

  • 请问您可以发布您尝试过的代码吗?另外,描述一下当你尝试时会发生什么?您是否收到任何类型的错误消息?
  • 我的意思是我没有收到任何错误消息我只是不知道如何使选定区域变亮和未选定区域变暗以显示不同
  • 检查 [this question][1] 以获取我在那里建议的替代库。 [1]:stackoverflow.com/questions/12758425/…
  • 请正确声明如何使用手动裁剪视图 ....
  • 检查此堆栈溢出答案:stackoverflow.com/questions/38367876/…

标签: android image android-crop


【解决方案1】:

你可以使用默认的安卓裁剪功能吗?

这是我的代码

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

这对我来说很容易实现,而且还显示了暗区。

【讨论】:

  • 嗨,我尝试了你的代码,但它仍然没有在我的手机上显示变暗区域:(我想知道这是因为我的清单吗?还是手机版本?
  • 你用的是哪个版本?
  • @domji84 你设置了 在你的清单文件中?
  • 在我的情况下,data.getExtras() 返回null,那我怎么能得到Bitmap
【解决方案2】:

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

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

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

您可以做多个customizations,例如将纵横比或形状设置为矩形、椭圆形等等。

【讨论】:

  • 我遇到了一个问题,裁剪后我的 uri 从 'file:///' 开始,但我希望 'content://' 在获取字符串路径时崩溃。
  • 裁剪后如何保存图片
  • 使用命令:mImageProfile.setImageBitmap(result.getBitmap()); 返回一个空白图像,我的意思是图像不可见。但是使用命令:mImageProfile.setImageURI(result.getUri()); 可以正常工作。
  • 你也可以从CustomAdapter开始,使用这个命令:CropImage.activity().setGuidelines(CropImageView.Guidelines.ON).start((AppCompatActivity) context);
  • 这个库现在没有维护。改用这个 -> link
【解决方案3】:

我发现了一个非常酷的库,试试这个。 这真的很流畅且易于使用。

https://github.com/TakuSemba/CropMe

【讨论】:

  • 你确实创建了那个库。这没有错,但至少不要试图误导人们认为你找到了它,或者考虑用免责声明发布它
  • 这个库没有Fragments 的正确使用说明。另外,作者对问题的回复也不好。
【解决方案4】:

希望你做得很好。 您可以使用我的代码裁剪图像。您只需创建一个类并将该类用于您的XMljava 类。 Crop image。 您可以将您选择的图像裁剪为圆形和方形,并有多种选择。 完全希望它对你有用。因为这对你来说是完全可以管理的,你可以根据你的情况进行更改。

享受你的工作:)

【讨论】:

  • 您的要点不包括 R.styleable. 很难让它工作,特别是对于字符串属性。
猜你喜欢
  • 2015-02-02
  • 1970-01-01
  • 2018-09-20
  • 2011-11-15
  • 1970-01-01
  • 1970-01-01
  • 2016-05-04
相关资源
最近更新 更多