繁体   English   中英

将位图裁剪为特定的宽度和高度而不会丢失分辨率

[英]Crop a bitmap to particular width and height without losing resolution

我需要从图库中裁剪图像并保存到服务器。 这是我的裁剪图像代码

   private void startCropImage() {

    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(mImageCaptureUri, "image/*");

    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
        //indicate output X and Y
    intent.putExtra("outputX", 400);
    intent.putExtra("outputY", 487);

    intent.putExtra("return-data", true);
    intent.putExtra("scale", true);
    startActivityForResult(intent, CROP_FROM_GALLERY);
}

我需要400 * 487的输出,但是在裁剪图像后,当我检查设备上的宽度仅为160时,但是在AVD上它显示了正确的宽度。 为什么会发生? 以下是我的onActivityResult代码

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bundle extras2 = data.getExtras();
if (extras2 != null) {

Bitmap bm2 = extras2.getParcelable("data");

imgview.setImageBitmap(bm2);

int width = bm2.getWidth();}

检查这一行:

Bitmap bm2 = extras2.getParcelable("data");

意向只能传输有限数量的数据。 根据版本或设备,它的范围可能从200kb到1 mb。 因此,此行返回裁剪图像的缩略图。 要获得完整图像,请执行以下操作。 请记住,您无法获得所需的确切宽度。

Uri selectedImage = data.getData();
        String[] filePath = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(
                           selectedImage, filePath, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String filePath = cursor.getString(columnIndex);
        cursor.close();
        Bitmap bmp= BitmapFactory.decodeFile(filePath);

您需要裁剪图像文件系统中的实际路径。 UPDATE com.android.camera.action.CROP不是官方的android intent。 像图库和相机之类的应用程序都支持它,但是很多设备不支持此意图。 看看@commonsware的这篇博客文章。 我已经在这篇SO帖子中使用库回答了裁剪图像的问题

我不知道您可能知道这个库是我的建议,只是我没有给出代码方面的答案,我给出了选择

为什么要重新发明轮子 :)(但我们当然可以改善)

https://github.com/edmodo/cropper

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM