4

サーブレットで2つの画像を読み取っていますが、両方を同時に表示する必要があります。現在、1つの画像のみが表示されています(最初に書き込まれた画像)。別の画像を書き込めません。エラーは発生しません。

私のサーブレットコードは次のようになります:

    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を使用して画像を書き込もうとしましたが、目的の画像を取得できませんでした。

4

1 に答える 1

3

2番目の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に3番目の色が追加されている理由は今のところわかりません。しかし、この例はあなたを助けることができると思います、そして私は私のコードをできるだけ早く修正します。

EDIT2:

変化 :

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

に :

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

そしてそれはうまくいくでしょう。

result.jpg:

ここに画像の説明を入力してください

于 2013-03-26T08:34:34.683 に答える