Hello,
Here is utility to convert JPG image to Bitmap image:
BufferedImage loadImg = ImageIO.read(new File("D:\\testimages\\test.jpg");
ImageIO.write(loadImg, "bmp", new File("D:\\testimages\\test.bmp"));
Problem with above conversion is, bitmap generated will always have 24bpp (bits per pixel).
I needed 1bpp bitmap image. Unfortunately google didn't help.
Here is what I tried,
BufferedImage loadImg = ImageIO.read(new File("D:\\testimages\\test.jpg"); //image to be converted to bmp
BufferedImage img = new BufferedImage(loadImg.getWidth(), loadImg
.getHeight(), BufferedImage.TYPE_BYTE_BINARY);
//create new image with type as TYPE_BYTE_BINARY for 1bpp, there are other options too for other bpp like 8, 24 etc
for (int y = 0; y < loadImg.getHeight(); ++y)
for (int x = 0; x < loadImg.getWidth(); ++x)
img.setRGB(x, y, loadImg.getRGB(x, y)); //copy RGB data from original image to new image
ImageIO.write(img, "bmp", new File("D:\\testimages\\test.bmp")); // now convert the above image to bmp
The above code gave me 1 bpp bitmap image.
Now image can be of any size, how to compress image to desired size ?
Here is answer:
//proved xfactor and y factor i.e width and heigth compression factor
AffineTransform tx = new AffineTransform();
tx.scale(xfact, yfact);
AffineTransformOp op = new AffineTransformOp(tx,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
You can calculate xfactor and yfactor from the desired width -height and actual width - height of the image.
More on image manipulation in java can be found at http://www.javalobby.org/articles/ultimate-image/
Thanks,
Anand
No comments:
Post a Comment