0

Please help some one for how to reduce the size of image in java

my problem is

i am taking one image as input for java program and taking output as .bmp image

for creating new image i am using the following code

for (int i = 0; i < width; i++)     
         {    
              for (int j = 0; j < height; j++)   
              {  
                Color c = new Color(color2[i][j], color2[i][j], color2[i][j]);  
                image1.setRGB(i, j, c.getRGB());   
              }     
          }   
ImageIO.write(image1, "bmp", new File("hh5_binary_Adjust.bmp"));

but if i use the above code i am getting 6.5 mb size for one A4 size paper so please help me to get a minimum length of image without modifying the file extension that is bmp

Ramya
  • 31
  • 8
  • possible duplicate of [Resizing a bitmap](http://stackoverflow.com/questions/9583757/resizing-a-bitmap) – DavidPostill Sep 05 '14 at 14:59
  • 2
    You cannot magically compress a format that does not support compression. Use PNG. – SLaks Sep 05 '14 at 14:59
  • @DavidPostill I think the one that you provided is about scaling the image and this one is about lowering the size(memory) of the image. – Christo S. Christov Sep 05 '14 at 15:01
  • @CodingChief It might be... it could be about either... the question is a little unclear. OP wants to reduce file size, but it's not obvious whether reducing resolution or changing format is acceptable. (Obviously one of these had better be acceptable...) – chiastic-security Sep 05 '14 at 15:03
  • If you scale it by 50% in both directions it will be around 4 times smaller in size (memory) ... – DavidPostill Sep 05 '14 at 15:03
  • From the file name, it seems your image is binary... If you create `image1` using `new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY)`, the file size will probably become a lot smaller than 6.5 MB. But as others have pointed out, if file size matters, BMP is not the format (try JBIG if image is binary). – Harald K Sep 05 '14 at 18:15
  • Following link help you refer this. http://stackoverflow.com/a/31745145/5129765 – Umesh Markande Jul 31 '15 at 11:44

1 Answers1

0

If you want to reduce the resolution, there isn't a good way to do this and get a really crisp output image without using an external library. I recommend imgscalr. I've had fantastic results with it.

But please be aware that the main reason you have a large file size is that you're using BMP as the output format! If you change that, you'll massively reduce the file size easily and cheaply. I'd go with JPG for photos, or PNG for graphics.

The ImageIO class will save to either of those with no trouble.

chiastic-security
  • 20,430
  • 4
  • 39
  • 67