2012-03-04 29 views
16

Tôi đã cố gắng tìm ra cách lật hình ảnh một lúc, nhưng chưa tìm ra.Flip Image với Graphics2D

Tôi đang sử dụng Graphics2D để vẽ một hình ảnh với

g2d.drawImage(image, x, y, null) 

Tôi chỉ cần một cách để lật hình ảnh trên trục ngang hoặc dọc.

Nếu bạn muốn, bạn có thể có một cái nhìn tại nguồn đầy đủ về github (link)

EDIT: Woops, tôi có nghĩa là để đặt 'lật' trong tiêu đề, chứ không phải 'xoay'.

+3

Thay vì ta hãy nhìn vào toàn bộ nguồn của bạn, làm cho một [SSCCE] (http://sscce.org) . – Jeffrey

+1

Nếu bạn google "Graphics2D xoay hình ảnh" bạn sẽ tìm thấy rất nhiều hướng dẫn – DNA

Trả lời

48

Từ http://examples.javacodegeeks.com/desktop-java/awt/image/flipping-a-buffered-image:

// Flip the image vertically 
AffineTransform tx = AffineTransform.getScaleInstance(1, -1); 
tx.translate(0, -image.getHeight(null)); 
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
image = op.filter(image, null); 


// Flip the image horizontally 
tx = AffineTransform.getScaleInstance(-1, 1); 
tx.translate(-image.getWidth(null), 0); 
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
image = op.filter(image, null); 

// Flip the image vertically and horizontally; equivalent to rotating the image 180 degrees 
tx = AffineTransform.getScaleInstance(-1, -1); 
tx.translate(-image.getWidth(null), -image.getHeight(null)); 
op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
image = op.filter(image, null); 
+1

+1 cho '-1' :-) Một ví dụ liên quan được đề cập [ở đây] (http://stackoverflow.com/a/9373195/230513) . – trashgod

3

Bạn có thể sử dụng biến đổi trên Graphics của mình, điều đó sẽ xoay hình ảnh tốt. Dưới đây là một số mẫu mã mà bạn có thể sử dụng để đạt được điều này:

AffineTransform affineTransform = new AffineTransform(); 
//rotate the image by 45 degrees 
affineTransform.rotate(Math.toRadians(45), x, y); 
g2d.drawImage(image, m_affineTransform, null); 
25

Các dễ nhất cách để lật hình ảnh là do tiêu cực nhân rộng nó. Ví dụ:

g2.drawImage(image, x + width, y, -width, height, null); 

Điều đó sẽ lật theo chiều ngang. Điều này sẽ lật nó theo chiều dọc:

g2.drawImage(image, x, y + height, width, -height, null); 
+11

Điều này gần như là tốt. Nó phải giống như g2.drawImage (hình ảnh, x + chiều cao, y, chiều rộng, chiều cao, null); lý do cho điều này là vì quy mô âm sẽ di chuyển hình ảnh sang trái, vì vậy bạn phải bù lại chuyển động này. –

+1

Yeah +1 @ T.G để cân bằng sự tiêu cực. –

+0

Tôi tin rằng điều này hiệu quả hơn việc sử dụng AffineTransform. Bất cứ ai cũng sửa tôi nếu tôi sai. (+1) – user3437460

0

Xoay hình ảnh theo chiều dọc 180 Degrees

File file = new File(file_Name); 
FileInputStream fis = new FileInputStream(file); 
BufferedImage bufferedImage = ImageIO.read(fis); //reading the image file   
AffineTransform tx = AffineTransform.getScaleInstance(-1, -1); 
tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null)); 
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); 
bufferedImage = op.filter(bufferedImage, null); 
ImageIO.write(bufferedImage, "jpg", new File(file_Name));