java rotate 90_2



转载

http://stackoverflow.com/questions/34459747/why-does-an-affine-rotation-in-90-270-degrees-not-preserve-correct-dimensions-b


import java.awt.geom.AffineTransform;import java.awt.image.AffineTransformOp;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;import javax.swing.Icon;import javax.swing.ImageIcon;import javax.swing.JOptionPane;import javax.swing.SwingUtilities;publicclass TestAffineTransform{ privatestatic finalString IMG_PATH ="https://upload.wikimedia.org/wikipedia/commons/thumb"+ "/4/42/MerryOldSanta.jpg/220px-MerryOldSanta.jpg";private staticBufferedImage originalImg;public staticvoid main(String[] args){ try{ URL imgUrl =new URL(IMG_PATH); originalImg= ImageIO.read(imgUrl);BufferedImage nextRotImg = transform(originalImg,1); final Icon originalIcon= newImageIcon(originalImg);final Icon nextRotIcon= newImageIcon(nextRotImg);SwingUtilities.invokeLater(()-> {JOptionPane.showMessageDialog(null, originalIcon);JOptionPane.showMessageDialog(null, nextRotIcon);}); }catch (IOException e){ e.printStackTrace();System.exit(-1);} }public staticBufferedImage transform(BufferedImage image,int numquadrants){ int w0= image.getWidth();int h0 = image.getHeight();int w1 = w0;int h1 = h0;int centerX = w0/ 2;int centerY = h0/ 2;if (numquadrants% 2== 1){ w1 = h0; h1= w0;if (numquadrants% 4== 1){ centerX =Math.max(w0, h0)/ 2;} else{ centerX =Math.min(w0, h0)/ 2;} centerY = centerX;} AffineTransform affineTransform= newAffineTransform(); affineTransform.setToQuadrantRotation(numquadrants, centerX, centerY);AffineTransformOp opRotated = newAffineTransformOp(affineTransform,AffineTransformOp.TYPE_BILINEAR);BufferedImage transformedImage = newBufferedImage(w1, h1, image.getType()); opRotated.filter(image, transformedImage);return transformedImage;}}







http://www.oschina.net/code/snippet_1466630_44221

http://www.oschina.net/code/snippet_818200_45972

http://blog.livedoor.jp/mgsoft/archives/1007874070.html




你可能感兴趣的:(java)