OSX 上的 JNI 问题

发布于 2024-08-13 15:38:10 字数 2507 浏览 8 评论 0原文

我在 OSX (10.5.8) 上使用 Java 6 (1.6.0_17 JVM: 14.3-b01-101) 上的 AWT 类时遇到问题。 尝试加载 java.awt.Dimension 代码会冻结,这种情况发生在 Eclipse 或命令行中。有人遇到同样的问题吗? JAI 在以下代码中使用该类:

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);
}

}

I'm running into problems using AWT classes on OSX (10.5.8) with Java 6 (1.6.0_17 JVM: 14.3-b01-101).
Trying to load java.awt.Dimension the code just freezes, this happens in Eclipse or from the command line. Anyone experiencing same problems ?
The class is used by JAI in the following code:

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);
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

负佳期 2024-08-20 15:38:10

您使用的是 Cocoa 版本的 Eclipse 吗? (相对于碳)如果是这样,这可能就是原因。请参阅此线程的讨论:Java getDefaultToolKit() 挂起 Mac OS X 10.5

Are you using the Cocoa version of Eclipse? (vs Carbon) If so, that may be the cause. See the discussion on this thread: Java getDefaultToolKit() hangs Mac OS X 10.5

小草泠泠 2024-08-20 15:38:10

您可以尝试从命令行设置“-Djava.awt.headless=true”标志,这将允许 AWT 类运行而无需初始化 GUI。

You might try setting the "-Djava.awt.headless=true" flag from the command line, which will allow AWT classes to run without having to initialize a GUI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文