转换带有透明背景原因的tif图像,黑色背景Java Jai

时间:2018-06-25 00:30:26

标签: java image tiff jai image-conversion

我尝试将背景格式为tiff的图像转换为背景透明,将其转换为jpeg,以将尺寸调整为200x200或1200x1200,但是转换时,背景变为黑色,我希望转换后背景保持透明或白色

我的代码如下:

public static void TiffToJpg(String tiff, String output) throws IOException {
        File tiffFile = new File(tiff);
        SeekableStream s = new FileSeekableStream(tiffFile);
        TIFFDecodeParam param = null;
        ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
        RenderedImage op = dec.decodeAsRenderedImage(0);
        FileOutputStream fos = new FileOutputStream(output);

        JPEGEncodeParam jpgparam = new JPEGEncodeParam();
        jpgparam.setQuality(100);
        ImageEncoder en = ImageCodec.createImageEncoder("jpeg", fos, jpgparam);
        en.encode(op);

        BufferedImage in = javax.imageio.ImageIO.read(new java.io.File("oi.jpg"));

        BufferedImage out = scaleImage(in, BufferedImage.TYPE_INT_RGB, 200, 200);
        javax.imageio.ImageIO.write(out, "JPG", new java.io.File("thumbnail.jpg"));

        BufferedImage out2 = scaleImage(in, BufferedImage.TYPE_INT_RGB, 1200, 1200);
        javax.imageio.ImageIO.write(out2, "JPG", new java.io.File("web-image.jpg"));

        fos.flush();
        fos.close();
    }

    public static void main(String[] args) throws Exception {
        Tiffthumbs.TiffToJpg("oi.tif", "oi.jpg");
    }

    public static BufferedImage scaleImage(BufferedImage image, int imageType, int newWidth, int newHeight) {
        // Make sure the aspect ratio is maintained, so the image is not distorted
        double thumbRatio = (double) newWidth / (double) newHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double aspectRatio = (double) imageWidth / (double) imageHeight;

        if (thumbRatio < aspectRatio) {
            newHeight = (int) (newWidth / aspectRatio);
        } else {
            newWidth = (int) (newHeight * aspectRatio);
        }

        // Draw the scaled image
        BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics2D = newImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, newWidth, newHeight, null);

        return newImage;
    }

如何在Java JAI中做到这一点?

1 个答案:

答案 0 :(得分:-1)

将比例尺图像功能更改为下面的代码,它应该可以工作。它所做的只是在绘制实际图像之前用白色重新绘制整个图像:

public static BufferedImage scaleImage(BufferedImage image, int imageType, int newWidth, int newHeight) {
        // Make sure the aspect ratio is maintained, so the image is not distorted
        double thumbRatio = (double) newWidth / (double) newHeight;
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);
        double aspectRatio = (double) imageWidth / (double) imageHeight;

        if (thumbRatio < aspectRatio) {
            newHeight = (int) (newWidth / aspectRatio);
        } else {
            newWidth = (int) (newHeight * aspectRatio);
        }

        // Draw the scaled image
        BufferedImage newImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D graphics2D = newImage.createGraphics();
        graphics2D.setColor(Color.WHITE);
        graphics2D.fillRect(0, 0, newWidth, newHeight);
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, newWidth, newHeight, null);

        return newImage;
    }

不变:

enter image description here

更改后:

enter image description here