Pregunta

Las imágenes JPEG que ImageIO generó vistas correctamente en el Explorador de archivos de Windows, así como Safari WebBrowser, pero en Firefox, las imágenes muestreadas están recortadas.

¿Cómo uso ImageIO sin corromper los resmectres?

El código debe cambiar el tamaño de la relación de aspecto de mantenimiento de la imagen, así como la compresión JPEG, la convertida en una matriz de byte [], que podría escribirse en un enchufe.

algunos de mi código. En este fragmento, intenté agregar la biblioteca JUI, pero sigue siendo el mismo problema.

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();
¿Fue útil?

Solución

He estado usando con éxito:

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

La transformación es simplemente escribir el contenido de la imagen a un nuevo BufferedImage

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top