裁剪从相机拍摄的大图像到640x640pixel

时间:2014-04-04 08:10:01

标签: android camera crop

我已经检查了很多讨论,但我似乎无法找到答案。如何裁剪相机拍摄的大图像并将其裁剪为640x640像素尺寸?我正在返回URI

编辑:我想允许用户裁剪图像!

4 个答案:

答案 0 :(得分:0)

使用以下代码

您也可以使用此链接作为参考

点击Crop image using rectengle

int targetWidth = 640;
int targetHeight = 640;
Bitmap targetBitmap = Bitmap.createBitmap(
    targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
Path path = new Path();
path.addRect(rectf, Path.Direction.CW);
canvas.clipPath(path);
canvas.drawBitmap(sourceBitmap,
    new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
    new Rect(0, 0, targetWidth, targetHeight), null);
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.setImageBitmap(targetBitmap);

答案 1 :(得分:0)

另一种解决方案是使用人们用来创建缩略图的createScaledBitmap。

    byte[] imageData = null;

    try     
    {

        final int THUMBNAIL_SIZE = 64;

        FileInputStream fis = new FileInputStream(fileName);
        Bitmap imageBitmap = BitmapFactory.decodeStream(fis);

        imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        imageData = baos.toByteArray();

    }
    catch(Exception ex) {

    }

您的位图imageBitmap可能必须直接来自您的相机而不是文件,但总的想法保持不变。

答案 2 :(得分:0)

您可以使用

private Bitmap crop(Bitmap src, int x, int y, int width, int height) {
    Bitmap dst = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dst);
    canvas.drawBitmap(src, new Rect(0, 0, src.getWidth(), src.getHeight()),
                           new Rect(x, y, width, height), null);
    return dst;
}

类型参数是不言自明的。

祝你好运。

答案 3 :(得分:0)

使用intent对象尝试此代码:

 intent.setType("image/*");
 intent.putExtra("outputX", int_Height_crop);
 intent.putExtra("outputY", int_Width_crop);
 intent.putExtra("aspectX", 1);
 intent.putExtra("aspectY", 1);
 intent.putExtra("scale", true);
相关问题