hupeng1991 / java-image-scaling

Automatically exported from code.google.com/p/java-image-scaling
Other
0 stars 0 forks source link

Create square pic #21

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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 sebastia...@gmail.com on 12 Jan 2011 at 8:57

GoogleCodeExporter commented 8 years ago

Original comment by m%nobel-...@gtempaccount.com on 5 Feb 2011 at 1:42

GoogleCodeExporter commented 8 years ago
+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 thomas.bruyelle@gmail.com on 3 Apr 2012 at 8:30