0

我尝试缩放图片,一开始效果很好,但是当缩小很多时,它似乎不起作用。一段时间后,它停止缩小。我无法在质量为 0 的情况下使字节数小于 630。我意识到这是一张糟糕的图片,但我想把它缩小很多,但它根本行不通。任何想法将不胜感激。

    ByteArrayOutputStream out;
    Bitmap bitmap = BitmapFactory.decodeStream(in);
    int width = bitmap.getWidth(); //3920
    int height = bitmap.getHeight(); //2204

    float scale = 0.0034; //usually calculated in runtime but set for simplicity now.

    // Resize the bitmap
    Matrix matrix = new Matrix();
    matrix.postScale(scale, scale);

    // Recreate the new bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);

    out = new ByteArrayOutputStream();
    resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 0, out);

    return out.toByteArray();
4

2 回答 2

0

你可以使用矩形来重新缩放你的位图

Rect srcrect = new Rect(0,0,yourbitmap.getWidth(),yourbitmap.getHeight());
Rect destrect = new Rect(startindexxright,startindexyright,startindexxleft,startindexyleft);
canvas.drawBitmap(yourbitmap, srcrect,destrect, null);

srcrect 是一个矩形,您必须在其中输入位图的详细信息,而 destrect 是设备显示的区域(您要显示图像的部分)。

于 2013-11-04T11:30:50.957 回答
0

检查BitmapFactory.Options.InSampleSize

此代码减少了存储在文件中的位图,但应该很容易将代码调整为内存中的位图

 public static Bitmap getReducedBitmap(Context context, String path) {    
    File bitmapFile = new File(path);

    Uri uri = Uri.fromFile(bitmapFile);
    InputStream in = null;

    ContentResolver contentResolver = context.getContentResolver();

    try {
      in = contentResolver.openInputStream(uri);

      // Decode image size
      BitmapFactory.Options options = new BitmapFactory.Options();
      options.inJustDecodeBounds = true;
      BitmapFactory.decodeStream(in, null, options);
      in.close();

      //scale works better with powers of 2
      int scale = 1;
      while ((options.outWidth * options.outHeight) * (1 / Math.pow(scale, 2)) > WSConfig.PICTURE_MAX_PIXELS) {
        scale++;
      }

      Bitmap reducedBitmap = null;
      in = contentResolver.openInputStream(uri);
      if (scale > 1) {
        // scale to max possible inSampleSize that still yields an image
        // larger than target
        options = new BitmapFactory.Options();
        options.inSampleSize = scale;
        reducedBitmap = BitmapFactory.decodeStream(in, null, options);    
      } else {
        reducedBitmap = BitmapFactory.decodeStream(in);
      }
      in.close();

      return reducedBitmap;
    } catch (IOException e) {
      Log.e("UTILS", e.getMessage(), e);
      return null;
    }
  }

我的 MAX_PICTURE_PICTURES 决定了最大分辨率

于 2013-11-04T11:46:58.403 回答