Package javax.imageio

Examples of javax.imageio.ImageWriteParam


public class ImageIOGenerator extends ImageGenerator {

    protected void write(ImageWrapper wrapper, ImageWriter writer, float quality, boolean alpha) throws IOException {
        BufferedImage bi = wrapper.getBufferedImage();
        // Set some parameters
        ImageWriteParam param = writer.getDefaultWriteParam();
        if (param.canWriteCompressed() &&
            quality >= 0.0 && quality <= 1.0) {
            param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            String[] types = param.getCompressionTypes();
            // If compression types are defined, but none is set, set the first one,
            // since setCompressionQuality, which requires MODE_EXPLICIT to be set,
            // will complain otherwise.
            if (types != null && param.getCompressionType() == null) {
                param.setCompressionType(types[0]);
            }
            param.setCompressionQuality(quality);
        }
        if (param.canWriteProgressive())
            param.setProgressiveMode(ImageWriteParam.MODE_DISABLED);
        // if bi has type ARGB and alpha is false, we have to tell the writer to not use the alpha channel:
        // this is especially needed for jpeg files where imageio seems to produce wrong jpeg files right now...
        if (bi.getType() == BufferedImage.TYPE_INT_ARGB
            && !alpha) {
            // create a new BufferedImage that uses a WritableRaster of bi, with all the bands except the alpha band:
View Full Code Here


   * @return the number if bytes written in the array
   * @throws IOException
   */
  protected static int writeJPEG(BufferedImage image,byte[] data) throws IOException {
    ImageWriter iw = (ImageWriter)ImageIO.getImageWritersByMIMEType("image/jpeg").next();
    ImageWriteParam iwParam = iw.getDefaultWriteParam();
    float quality = .8f;
    iwParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    iwParam.setCompressionQuality(quality);
    CustomByteArrayOutputStream out = new CustomByteArrayOutputStream(data);
    iw.setOutput(out);
   
    IIOImage img = new IIOImage(image, null, null);
   
View Full Code Here

                                 ImageWriter writer,
                                 OutputStream out,
                                 IIOImage image)
           throws IOException
   {
      ImageWriteParam param;
      if (mediaType.equals(MediaType.valueOf("image/jpeg")))
      {
         param = new JPEGImageWriteParam(Locale.US);
      }
      else
      {
         param = writer.getDefaultWriteParam();
      }

      /*
      * If the image output type supports compression, set it to the highest
      * maximum
      */
      ImageWriterParams writerParams =
              FindAnnotation.findAnnotation(annotations, ImageWriterParams.class);
      if (writerParams != null)
      {
         if (param.canWriteCompressed())
         {
            param.setCompressionMode(writerParams.compressionMode());
            param.setCompressionQuality(writerParams.compressionQuality());
         }
      }
      else if (param.canWriteCompressed())
      {
         param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
         param.setCompressionQuality(1.0f);
      }
      BufferedOutputStream buff = new BufferedOutputStream(out, 2048);
      ImageOutputStream ios = ImageIO.createImageOutputStream(buff);
      try
      {
View Full Code Here

           
            //param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            //param.setCompressionQuality(ClientConnectionBean.imgQualityDefault);

           
            ImageWriteParam iwparam = new JPEGImageWriteParam( Locale.getDefault() );
            iwparam.setCompressionMode( ImageWriteParam.MODE_EXPLICIT ) ;
            iwparam.setCompressionQuality( ClientConnectionBean.imgQualityDefault );
            writer.write( null, new IIOImage(image, null, null), null );
           
            ios.flush();
            writer.dispose();
            ios.close();
View Full Code Here

                if (iter.hasNext())
                {
                    ImageWriter writer = (ImageWriter)iter.next();
                    if (Main.m_settings.isDebug())
                        System.err.println(writer.getClass().getName());
                    ImageWriteParam iwp = writer.getDefaultWriteParam();
                    iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                    iwp.setCompressionQuality((float)quality);
                    if (bOptimizedHuffman && iwp instanceof JPEGImageWriteParam)
                    {
                        JPEGImageWriteParam iwpjpeg = (JPEGImageWriteParam)iwp;
                        // this can reduce size, but can also break badly written decoders
                        iwpjpeg.setOptimizeHuffmanTables(true);
View Full Code Here

                        while( writerIter.hasNext() && !foundWriter )
                        {
                            try
                            {
                                imageWriter = (ImageWriter)writerIter.next();
                                ImageWriteParam writerParams = imageWriter.getDefaultWriteParam();
                                if(writerParams.canWriteCompressed() )
                                {
                                    writerParams.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
                                    writerParams.setCompressionQuality(1.0f);
                                }
                               
                               
                                imageWriter.setOutput( output );
                                imageWriter.write( null, new IIOImage( image, null, null), writerParams );
View Full Code Here

   *            质量 0.1f ~ 1.0f
   */
  public static void writeJpeg(RenderedImage im, File targetJpg, float quality) {
    try {
      ImageWriter writer = ImageIO.getImageWritersBySuffix("jpg").next();
      ImageWriteParam param = writer.getDefaultWriteParam();
      param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      param.setCompressionQuality(quality);
      ImageOutputStream os = ImageIO.createImageOutputStream(targetJpg);
      writer.setOutput(os);
      writer.write((IIOMetadata) null, new IIOImage(im, null, null), param);
    }
    catch (IOException e) {
View Full Code Here

  protected boolean exportAsJPG(final File file) {
    final BufferedImage rendImage = createRenderedImage();
    boolean success = false;

    try {
      final ImageWriteParam iwparam   = new JPEGImageWriteParam(Locale.getDefault());
      final ImageWriter iw       = ImageIO.getImageWritersByFormatName("jpg").next();//$NON-NLS-1$
      try(final ImageOutputStream ios = ImageIO.createImageOutputStream(file)){
        iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwparam.setCompressionQuality(1f-dialogueBox.getCompressionRate()/100f);
        iw.setOutput(ios);
        iw.write(null, new IIOImage(rendImage, null, null), iwparam);
        iw.dispose();
        success = true;
      }
View Full Code Here

  protected boolean exportAsBMP(final File file){
    final BufferedImage rendImage = createRenderedImage();
    boolean success = false;

    try {
      final ImageWriteParam iwparam  = new BMPImageWriteParam();
      final ImageWriter iw      = ImageIO.getImageWritersByFormatName("bmp").next();//$NON-NLS-1$
      try(final ImageOutputStream ios  = ImageIO.createImageOutputStream(file)) {
        iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iw.setOutput(ios);
        iw.write(null, new IIOImage(rendImage, null, null), iwparam);
        iw.dispose();
        success = true;
      }
View Full Code Here

      // Drawing the template
      graphic2.drawImage(bufferImage, aff, null);

      // Creation of the png file with the second picture
      final ImageWriteParam iwparam = new JPEGImageWriteParam(Locale.getDefault());
      final ImageWriter iw = ImageIO.getImageWritersByFormatName("png").next();//$NON-NLS-1$

      iwparam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      iwparam.setCompressionQuality(1);

      try {
        try(final ImageOutputStream ios = ImageIO.createImageOutputStream(templateFile)){
          iw.setOutput(ios);
          iw.write(null, new IIOImage(bufferImage2, null, null), iwparam);
View Full Code Here

TOP

Related Classes of javax.imageio.ImageWriteParam

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.