【问题标题】:crop large image taken from camera to 640x640pixel将相机拍摄的大图像裁剪为 640x640 像素
【发布时间】:2014-04-04 08:10:01
【问题描述】:

我检查了很多讨论,但似乎找不到答案。如何裁剪相机拍摄的大图像并将其裁剪为 640x640 像素大小?我返回一个URI

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

【问题讨论】:

  • 您是否使用 Intent 从相机捕获图像?
  • 是的,我是。 MediaStore.ACTION_IMAGE_CAPTURE

标签: android camera crop


【解决方案1】:

另一种解决方案是使用人们用来创建缩略图的 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】:

    你可以使用

    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】:

      试试这个代码,使用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);
      

      【讨论】:

        【解决方案4】:

        使用下面的代码

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-30
          • 1970-01-01
          • 2010-12-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多