【问题标题】:jpegs are corrupted when resampled with java imageIO使用 java imageIO 重新采样时 jpeg 已损坏
【发布时间】:2011-01-13 21:29:41
【问题描述】:

ImageIO 生成的 JPEG 图像在 windows 文件资源管理器和 safari webbrowser 上正确查看,但在 FireFox 中,重新采样的图像被剪切。

如何在不破坏重采样的情况下使用 ImageIO?

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

我的一些代码。在这个 sn-p 中,我尝试添加 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();

【问题讨论】:

    标签: java firefox jpeg javax.imageio resampling


    【解决方案1】:

    我已经成功使用了:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-18
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多