0

我正在使用以下示例代码,https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop。它给出了固定大小的裁剪视图(即 HighlightView)。但问题是这个 highlightView 是固定的取决于上传的图像。下面是两个不同大小的上传图像的屏幕截图。

我使用下面的代码行来修复 HighlightView 的大小:

 private void beginCrop(Uri source) {
    Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped"));
    new Crop(source).output(outputUri).asRectangle().withFixedSize(100, 210).start(this);
 }

withFixedSize() 方法用于固定裁剪区域的大小,如果我们使用此方法,我们无法调整该视图的大小,这很好。但是那个裁剪区域应该固定为宽度=100,高度=210,它不应该改变取决于上传的图像。

大尺寸上传的图片有小尺寸的裁剪视图(即HighlightView):

在此处输入图像描述

小尺寸上传的图片有大尺寸的裁剪视图(即HighlightView):

在此处输入图像描述

我的要求是我必须修复裁剪区域的大小,我不应该调整它的大小。我在谷歌世界搜索。但我没有找到解决方案。

请帮助我。

4

1 回答 1

0

访问: https ://github.com/jdamcd/android-crop

庄稼

Crop.of(inputUri, outputUri).asSquare().start(activity)

收听裁剪的结果(如果您想做一些错误处理,请参见示例项目):

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) {
        doSomethingWithCroppedImage(outputUri);
    }
}

提供了一些属性来自定义裁剪屏幕。请参阅示例项目主题。

挑选

该库提供了一种实用方法来启动图像选择器:

Crop.pickImage(activity)

依赖

AAR 发布在 Maven Central 上:

compile 'com.soundcloud.android:android-crop:1.0.1@aar'

Java代码:

public class MainActivity extends Activity {

    private ImageView resultView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        resultView = (ImageView) findViewById(R.id.result_image);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_select) {
            resultView.setImageDrawable(null);
            Crop.pickImage(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
            beginCrop(result.getData());
        } else if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, result);
        }
    }

    private void beginCrop(Uri source) {
        Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"));
        Crop.of(source, destination).asSquare().start(this);
    }

    private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            resultView.setImageURI(Crop.getOutput(result));
        } else if (resultCode == Crop.RESULT_ERROR) {
            Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
        }
    }

}

输出 :

在此处输入图像描述

于 2016-05-11T12:42:19.087 回答