java - 使用 java imageIO 重新采样时 jpeg 被损坏

标签 java firefox jpeg javax.imageio resampling

ImageIO 生成的 JPEG 图像可以在 Windows 文件资源管理器以及 safari 网络浏览器上正确查看,但在 FireFox 中,重新采样的图像会被剪裁。

如何使用 ImageIO 而不破坏重新采样?

代码应该调整图像大小并保持纵横比,并进行 jpeg 压缩,将其转换为可以写入套接字的字节 [] 数组。

我的一些代码。在此代码片段中,我尝试添加 Jui 库,但仍然存在同样的问题。

public static BufferedImage imageistream;

public void Resample(String child,double width,double height) throws Exception, InvalidFileStructureException, InvalidImageIndexException, UnsupportedTypeException, MissingParameterException, WrongParameterException
{
    String imagePath = "";
    if(this.is_mac_unix == true)
    {
        imagePath = this.path+"/"+child;
    }
    else
    {
        imagePath = this.path+"\\"+child;
    }       
        PixelImage bmp = null;
        try {
            bmp = ToolkitLoader.loadViaToolkitOrCodecs(imagePath, true, null);              
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Resample resample = new Resample();
        resample.setInputImage(bmp);
        double fixedRatio = width/height;

        if(((double)bmp.getWidth()/bmp.getHeight()) >= fixedRatio)
        {

        resample.setSize((int)width,(int)(bmp.getHeight()/(bmp.getWidth()/width)));

        }
        else
        {
            resample.setSize((int)width,(int)(bmp.getWidth()/(bmp.getHeight()/height)));    

        }
        resample.setFilter(Resample.FILTER_TYPE_LANCZOS3);
        resample.process();
        PixelImage scaledImage = resample.getOutputImage();         
        Processor.imageistream = ImageCreator.convertToAwtBufferedImage(scaledImage);
        bmp = null;              
        Runtime rt = Runtime.getRuntime();
        rt.gc();                        
}


         ...


      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try {
        ImageIO.write(Processor.imageistream, "jpg", baos);
                    // ImageIO.write(Processor.imageistream, "png", baos); Works!
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    byte bytes[] = baos.toByteArray();

    ByteArrayInputStream is = new ByteArrayInputStream(bytes);

      OutputStream os = (OutputStream)obj[1];
      OutputStreamWriter writer = (OutputStreamWriter)obj[0];

      byte[] buf= new byte[4096];
      int c;

      try {

      while (true) {
    c= is.read(buf);
    if (c<= 0)  break;
    os.write(buf, 0, c);

      }
      writer.close();
      os.close();
      is.close();

最佳答案

我已经成功使用:

BufferedImage bufferedImage = ImageIO.read(..);
Image img = bufferedImage.getScaledInstance(..);
BufferedImage result = // transform Image to BufferedImage
ImageIO.write(result, "image/jpeg", response.getOutputStream());

转换只是将图像的内容写入新的BufferedImage

关于java - 使用 java imageIO 重新采样时 jpeg 被损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2213681/

相关文章:

internet-explorer-7 - 为什么 IE7 指定 mime 类型为 image/pjpeg 而不仅仅是 image/jpeg?

iPhone JPG 图像有非标准魔术字节 ff d8 ff e1?

java - 某些字段的 jackson JSON 自定义序列化

java - 如何使用 Selenium/Java 验证页面中元素的存在

git - 如何配置 git help 以使用 Firefox?

ffmpeg - RTP/MJPEG 流的默认量化表是什么?

java - 需要调用aspect而无需xml配置

java - 测试依赖时间的服务方法

firefox - 很棒的字体在 Firefox 中不起作用

javascript - 在默认的 Firefox Webconsole 中打开响应并再次发送