2009-12-09 92 views
2

我遇到了使用Java 6(1.6.0_17 JVM:14.3-b01-101)在OSX(10.5.8)上使用AWT類的問題。 嘗試加載java.awt.Dimension,代碼剛剛凍結,這發生在Eclipse或命令行中。任何遇到同樣問題的人? 該類在下面的代碼使用JAI:OSX上的JNI問題

public static byte[] resizeAsJPG(byte[] imageContent, double scale, float outputQuality) throws IllegalArgumentException, 
    ImageOperationException { 
if (scale <= 0) { 
    throw new IllegalArgumentException("scale must be a positive number"); 
} 
if (outputQuality <= 0 || outputQuality > 1.0F) { 
    throw new IllegalArgumentException("outputQuality must be between 0 and 1"); 
} 
try { 
    // Fetch input image to seekable stream 
    RenderedOp originalImage = getRenderedOp(imageContent); 
    ((OpImage) originalImage.getRendering()).setTileCache(null); 

    // Set scale parameters 
    ParameterBlock saclingParams = new ParameterBlock(); 
    saclingParams.addSource(originalImage); // The source image 
    saclingParams.add(scale); // The xScale 
    saclingParams.add(scale); // The yScale 
    saclingParams.add(0.0); // The x translation 
    saclingParams.add(0.0); // The y translation 

    // RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
    Map<RenderingHints.Key, Object> renderingHints = new HashMap<RenderingHints.Key, Object>(); 
    renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 
    renderingHints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); 
    renderingHints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); 
    renderingHints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 

    // Scale using sub-sampling average which provides much better quality than bicubic interpolation 
    RenderedOp scaledImage = JAI.create("SubsampleAverage", saclingParams, new RenderingHints(renderingHints)); 

    // Encode scaled image as JPEG 
    JPEGEncodeParam encodeParam = new JPEGEncodeParam(); 
    encodeParam.setQuality(outputQuality); 

    // Since we scale height and width (don't take into account the quality) 
    int outputSizeEstimate = (int) (imageContent.length * scale * scale); 
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(outputSizeEstimate); 
    ImageEncoder encoder = ImageCodec.createImageEncoder("JPEG", outputStream, encodeParam); 
    encoder.encode(scaledImage); 
    return outputStream.toByteArray(); 
} catch (Exception e) { 
    throw new ImageOperationException(e.getMessage(), e); 
} 

}

回答

0

您可以嘗試在命令行中,這將使AWT類設置「-Djava.awt.headless =真正的」標誌無需初始化GUI即可運行。

+0

這並沒有幫助 – talg 2009-12-12 01:09:12