java-image-scaling
java-image-scaling copied to clipboard
Create square pic
What steps will reproduce the problem?
1. not depending if a pic is 300x100px or 100x300px
2. I want to create a pic with eg. 300x300px or 60x60px
3. filling the rest of the background with white (or transparent) color
What is the expected output? What do you see instead?
I expect the output to be a square pic. what I get is a pic with the format of
a pic that I input (input300x100px output: 150x50px expected 150x150px)
What version of the product are you using? On what operating system?
0.8.5
Please provide any additional information below.
Original issue reported on code.google.com by [email protected] on 12 Jan 2011 at 8:57
Original comment by m%[email protected] on 5 Feb 2011 at 1:42
- Changed state: Accepted
- Added labels: Type-Enhancement
- Removed labels: Type-Defect
+1 it would be nice to create square with black border if the the original
image is not a square.
The following code works great for me :
ResampleOp resampleOp = new ResampleOp( destWidth, destHeight );
resampleOp.setUnsharpenMask( AdvancedResizeOp.UnsharpenMask.Normal );
BufferedImage destImage = resampleOp.filter( sourceImage, null );
ImageIO.write( destImage, "JPG", "rescaled.jpg" );
//Generate square image
BufferedImage resImage = ImageIO.read( "rescaled.jpg" );
BufferedImage resultImage =
new BufferedImage( imageSize.longSide, imageSize.longSide, BufferedImage.TYPE_INT_RGB );
Graphics2D resultGraphics = resultImage.createGraphics();
double translateX = ( imageSize.longSide - destWidth ) / 2;
double translateY = ( imageSize.longSide - destHeight ) / 2;
AffineTransform resultAffineTransform = AffineTransform.getTranslateInstance( translateX, translateY );
resultGraphics.drawRenderedImage( resImage, resultAffineTransform );
ImageIO.write( resultImage, "PNG", "square.png" );
resultGraphics.dispose();
Original comment by [email protected] on 3 Apr 2012 at 8:30