在Servlet中将多个图像写入输出流

4

我正在servlet中读取两个图像,并需要同时显示两个。 目前,只显示一个图像(写在前面的)无法写入另一个图像。 我没有收到任何错误。

我的servlet代码如下:

    BufferedImage buffImageA = ImageIO.read(getServletContext().getResourceAsStream("/images/3520276097315A.jpg"));
    BufferedImage buffImageB = ImageIO.read(getServletContext().getResourceAsStream("/images/3520276097315B.jpg"));

    logger.logDebug("Images has been read");

    watermark(buffImageA,ApplicationConfig.WATERMARK_TEXT);
    watermark(buffImageB,ApplicationConfig.WATERMARK_TEXT);

    byte[] resultDataA = encodeJPEG(buffImageA, 100);
    byte[] resultDataB = encodeJPEG(buffImageB, 100);

    byte[] combinedImage = new byte[resultDataA.length+resultDataB.length];

    for(int i=0; i<resultDataA.length ;i++){
        combinedImage[i] = resultDataA[i];
    }

    for(int i=resultDataA.length; i<resultDataB.length ;i++){
        combinedImage[i] = resultDataB[i];
    }

    response.setContentType("image/jpeg");

    response.setContentLength(resultDataA.length + resultDataB.length);
    OutputStream os = response.getOutputStream();
    os.write(combinedImage);
    os.close();

//水印处理在此进行

private void watermark(BufferedImage original, String watermarkText) {

}

private byte[] encodeJPEG(BufferedImage image, int quality) throws IOException {
       ByteArrayOutputStream baos = new ByteArrayOutputStream((int) ((float) image.getWidth() * image.getHeight() / 4));
       JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(baos);
       JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
       quality = Math.max(0, Math.min(quality, 100));
       param.setQuality((float) quality / 100.0f, false);
       encoder.setJPEGEncodeParam(param);
       encoder.encode(image);
       byte[] result = baos.toByteArray();
       baos.close();
       return result;
   }

我曾尝试使用ImageIO.write写入图片,但未能得到所需的结果。

1个回答

3
你的第二个for循环必须像这样:

for循环应该按照下面的格式编写:

for(int i=resultDataA.length; i<resultDataB.length+resultDataA.length ;i++){
    combinedImage[i] = resultDataB[i-resultDataA.length];
}

编辑:

下面是一个可编译、可运行的示例,与您期望的接近:

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.awt.Graphics;

public class Essai2 {

    public static void main(String[] args) {

        try {

            byte[] imageInByte;
            BufferedImage originalImage1 = ImageIO.read(new File("essai1.jpg"));
            BufferedImage originalImage2 = ImageIO.read(new File("essai2.jpg"));

            // convert BufferedImage to byte array
            ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
            ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
            ImageIO.write(originalImage1, "jpg", baos1);
            ImageIO.write(originalImage2, "jpg", baos2);
            baos1.flush();
            baos2.flush();
            byte[] ba1 = baos1.toByteArray();
            byte[] ba2 = baos2.toByteArray();
            imageInByte = new byte[ba1.length + ba2.length];
            //System.out.println(new String(imageInByte));
            System.arraycopy(ba1, 0, imageInByte, 0, ba1.length);
            //System.out.println(new String(imageInByte));
            System.arraycopy(ba2, 0, imageInByte, ba1.length, ba2.length);
            //System.out.println(new String(imageInByte));
            baos1.close();
            baos2.close();

            // convert byte array back to BufferedImage
            InputStream in = new ByteArrayInputStream(imageInByte);

            int w = Math.max(originalImage1.getWidth(), originalImage2.getWidth());
            //int h = Math.max(originalImage1.getHeight(), originalImage2.getHeight());
            int h = originalImage1.getHeight() + originalImage2.getHeight();
            BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            //BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR );

            //BufferedImage bImageFromConvert = ImageIO.read(in);

            Graphics g = bImageFromConvert.getGraphics();
            g.drawImage(originalImage1, 0, 0, null);
            g.drawImage(originalImage2, 0, originalImage1.getHeight(), null);

            ImageIO.write(bImageFromConvert, "jpg", new File("result.jpg"));

        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

essai1.jpg:

输入图像描述

essai2.jpg:

输入图像描述

result.jpg:

输入图像描述

目前我还没有找到为什么在result.jpg中添加了第三种颜色的原因。但是我认为这个示例可以帮助您,我将尽快修复我的代码。

编辑2:

更改:

BufferedImage bImageFromConvert = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);

收件人:

BufferedImage bImageFromConvert = new BufferedImage(w, h, originalImage1.getType());

它将正常工作。

result.jpg:

这里输入图片描述


我想知道是否可以像上面的代码一样附加两个图像。我认为它没有显示是因为在字节写入时第一个图像的文件结束符出现,它立即终止写入过程并关闭流吗? - usman
谢谢您的帮助。但我仍然有一个问题,为什么不能直接从OutputStream中写入?这是文件结束的问题吗? - usman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接