title: Compress 2d graphics written image size in java
body: <p>I have an image and using <code>java.awt.Graphics2D</code> in java I have written a text on the image. Everything works fine until here. When I tried to compress this image in java using <code>ImageWriteParam</code> in java the picture gets overblown. Below is the code which I used to compress the image size. This code works fine for normal images though.</p>
  
  <pre><code>    File input = new File("Input Image File");
      BufferedImage image = ImageIO.read(input);
  
      File compressedImageFile = new File("Compressed image file location");
      OutputStream os = new FileOutputStream(compressedImageFile);
  
      Iterator&lt;ImageWriter&gt; writers = ImageIO.getImageWritersByFormatName("jpg");
      ImageWriter writer = (ImageWriter) writers.next();
  
      ImageOutputStream ios = ImageIO.createImageOutputStream(os);
      writer.setOutput(ios);
  
      ImageWriteParam param = writer.getDefaultWriteParam();
  
      param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
      param.setCompressionQuality(1.0f);  // Change the quality value you prefer
      try {
          writer.write(null, new IIOImage(image, null, null), param);
          System.out.println(writer);
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      os.close();
      ios.close();
      writer.dispose();
  </code></pre>
  
  <p>Any help is appreciated. Thanks !</p>