Отразить изображение с Graphics2D - PullRequest
20 голосов
/ 05 марта 2012

Я пытался выяснить, как перевернуть изображение, но пока не понял.

Я использую Graphics2D, чтобы нарисовать Image с

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

Мне просто нужен способ перевернуть изображение по горизонтальной или вертикальной оси.

Если хотите, можете посмотреть полный источник на github .

Ответы [ 5 ]

53 голосов
/ 05 марта 2012

С 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);
28 голосов
/ 11 ноября 2013

Самый простой способ перевернуть изображение - отрицательное масштабирование.Пример:

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

Это перевернет его по горизонтали.Это перевернет его вертикально:

g2.drawImage(image, x, y + height, width, -height, null);
3 голосов
/ 05 марта 2012

Вы можете использовать преобразование на Graphics, которое должно вращать изображение очень хорошо. Ниже приведен пример кода, который вы можете использовать для достижения этой цели:

AffineTransform affineTransform = new AffineTransform(); 
//rotate the image by 45 degrees 
affineTransform.rotate(Math.toRadians(45), x, y); 
g2d.drawImage(image, m_affineTransform, null); 
0 голосов
/ 17 ноября 2018

Вам необходимо знать ширину и высоту изображения, чтобы убедиться, что оно правильно масштабировано:

int width = image.getWidth(); int height = image.getHeight();

Затем вам нужно нарисовать его:

//Flip the image both horizontally and vertically
g2d.drawImage(image, x+(width/2), y+(height/2), -width, -height, null);
//Flip the image horizontally
g2d.drawImage(image, x+(width/2), y-(height/2), -width, height, null);
//Flip the image vertically
g2d.drawImage(image, x-(width/2), y+(height/2), width, -height, null);

Вот какВ любом случае, я делаю это.

0 голосов
/ 06 июля 2015

Поворот изображения по вертикали на 180 градусов

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));
...