使用Java Advanced Imaging api输出错误(monocolor)

时间:2014-07-19 17:37:44

标签: java java-2d imaging jai

我有一个示例图像(抱歉图像类型),我将其输入JAI代码以获取压缩图像。

现在输出的图像是单色的。我不知道为什么输出异常但其他图像处理得很好。

样本原始图像和处理过的图像是 -

原始图片 -

sample image

已处理图片 -

Processed Image

处理图像的JAI代码 -

private static final String JAI_STREAM_ACTION = "stream";

private static final String JAI_SUBSAMPLE_AVERAGE_ACTION = "SubsampleAverage";

private static final String JAI_ENCODE_FORMAT_JPEG = "JPEG";

private static final String JAI_ENCODE_ACTION = "encode";


private static final String JPEG_CONTENT_TYPE = "image/jpeg";
private int mMaxWidth = 800;

//private int mMaxWidthThumbnail = 150;

private byte[] resizeImageAsJPG(byte[] pImageData, int pMaxWidth) throws IOException {

    InputStream imageInputStream = new ByteArrayInputStream(pImageData);

    SeekableStream seekableImageStream = SeekableStream.wrapInputStream(imageInputStream, true);
    RenderedOp originalImage = JAI.create(JAI_STREAM_ACTION, seekableImageStream);
    ((OpImage) originalImage.getRendering()).setTileCache(null);
    int origImageWidth = originalImage.getWidth();

    double scale = 1.0;
    /*
    if (pMaxWidth > 0 && origImageWidth > pMaxWidth) {
        scale = (double) pMaxWidth / originalImage.getWidth();
    } */
    ParameterBlock paramBlock = new ParameterBlock();
    paramBlock.addSource(originalImage); // The source image
    paramBlock.add(scale); // The xScale
    paramBlock.add(scale); // The yScale
    paramBlock.add(0.0); // The x translation
    paramBlock.add(0.0); // The y translation

    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_QUALITY);

    RenderedOp resizedImage = JAI.create(JAI_SUBSAMPLE_AVERAGE_ACTION, paramBlock, qualityHints);
    BufferedImage scaledImage = null ;

    ByteArrayOutputStream encoderOutputStream = new ByteArrayOutputStream();
    JAI.create(JAI_ENCODE_ACTION, resizedImage, encoderOutputStream, JAI_ENCODE_FORMAT_JPEG, null);    
    //byte[] resizedImageByteArray = encoderOutputStream.toByteArray(); 
    System.out.println("This is from exiting JAI");
    return encoderOutputStream.toByteArray();
}

使用imgScalr我也得到相同的输出。

在使用JAI的openJDK中我收到错误 -

javax.media.jai.util.ImagingException: All factories fail for the operation "encode"

在openJDK中使用imgScalr -

javax.imageio.IIOException: Invalid argument to native writeImage

我可以在java中使用任何其他库来获得所需的结果。

此致

1 个答案:

答案 0 :(得分:0)

解决方案是将图像转换为3Byte_BGR -

private synchronized BufferedImage getScaledImage1(BufferedImage imageBytes)
{
    BufferedImage scaledImage = null;
    int type = 0;
    try
    {
        type = imageBytes.getType();
        if(type == 0 || type ==6) {
            int w = imageBytes.getWidth();
            int h = imageBytes.getHeight();
            BufferedImage newImage = 
            new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
            Graphics2D g = newImage.createGraphics();
            g.drawImage(imageBytes, 0, 0, null);
            g.dispose();
            imageBytes = newImage;
        }
        scaledImage = Scalr.resize(imageBytes, Scalr.Method.ULTRA_QUALITY,2000,
        Scalr.OP_ANTIALIAS);
    } catch(Exception e) {        
    }        
    return scaledImage;
}