我想调整图像大小并使用以下代码进行操作:
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 并且没有调整大小。