1

下面的代码调整图像大小。不幸的是,垂直图像的图像两侧有黑条。看起来好像透明的或空白的空间被黑色填充了。我尝试将背景颜色设置为白色,并使用 alphaRGB 但似乎无法动摇它。

    OrderProductAssetEntity orderProductAssetEntity = productAssets.get(jobUnitEntity.getId());
    File asset = OrderProductAssetService.getAssetFile(orderProductAssetEntity);
    if (asset.exists()) {
        //resize the asset to a smaller size
        BufferedImage resizedImage = new BufferedImage(200, 200, BufferedImage.TYPE_INT_RGB);
        Graphics2D g = resizedImage.createGraphics();
        g.setBackground(Color.WHITE);
        g.drawImage(ImageIO.read(asset), 0, 0, width, height, null);
        g.dispose();

        jobUnitImages.put(orderProductAssetEntity.getOriginalLocation(), new PDJpeg(document, resizedImage));
    } else {
        jobUnitImages.put(orderProductAssetEntity.getOriginalLocation(), null);
    }
4

1 回答 1

3

首先,如果你需要透明度,那BufferedImage.TYPE_INT_RGB行不通,你需要BufferedImage.TYPE_INT_ARGB. 不确定您是否已经尝试过,只是想清楚一点。

二、这一行:

g.setBackground(Color.WHITE);

...只为图形上下文设置当前背景颜色。它不会该颜色填充背景。为此,您也需要这样做g.clearRect(0, 0, width, height)。但我通常更喜欢使用g.setColor(...)andg.fillRect(...)来避免混淆。

或者,如果您愿意,也可以使用drawImage将 aColor作为倒数第二个参数的方法,如下所示:

g.drawImage(image, 0, 0, width, height, Color.WHITE, null);

编辑:

第三,类名PDJpeg意味着图像稍后会以 JPEG 格式存储,因此无论如何您都可能会失去透明度。所以最好的可能是坚持TYPE_INT_RGB,并用正确的颜色填充背景(并以正确的方式做;-)。

于 2013-09-28T12:07:34.453 回答