BitmapFactory.Options inSampleSize裁剪图像

时间:2013-10-01 20:52:01

标签: android

我想调整图片大小,并使用以下代码进行操作:

    public Bitmap createWatermark(Bitmap src, String watermark, int x, int y) {
    Log.d("Original Dimensions", src.getWidth() + " x " + src.getHeight());

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, 30, stream);
    byte[] byte_arr = stream.toByteArray();
    Bitmap result = BitmapFactory.decodeByteArray(byte_arr, 0, byte_arr.length, options).copy(Bitmap.Config.ARGB_8888, true);
    Log.d("Result Dimensions", result.getWidth() + " x " + result.getHeight());

    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(src, 0, 0, null);

    Paint paint = new Paint();
    paint.setColor(Color.YELLOW);
    paint.setTextSize(40);
    canvas.drawText(watermark, x, y, paint);
    paint.setColor(Color.BLACK);
    canvas.drawText(watermark, x, y+50, paint);
    return result;
}

日志如下:

D /原始尺寸(11180):1920 x 2560

D /结果尺寸(11180):960 x 1280

但返回的图像被裁剪为960 x 1280并且没有重新调整大小。

1 个答案:

答案 0 :(得分:0)

尝试:

Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
myImageView.setImageBitmap(Bitmap.createScaledBitmap(b, 960, 1280, false));