黑马程序员技术交流社区
标题:
java实现图片的旋转,可为什么只能旋转90度,不能旋转180度270度
[打印本页]
作者:
转身大笑面对
时间:
2013-6-7 12:16
标题:
java实现图片的旋转,可为什么只能旋转90度,不能旋转180度270度
核心代码如下:
旋转操作 每按一次旋转90度
void xuanzhuan_actionPerformed(ActionEvent e){
try {
Image ia =ic.getImage();
int width = ia.getWidth(this);
int height = ia.getHeight(this);
int pixels[] = new int [width * height];
PixelGrabber pg = new PixelGrabber(ia, 0, 0, width, height, pixels, 0, width);
if (pg.grabPixels() && ((pg.status()& ImageObserver.ALLBITS) !=0)) {
image1 = createImage(new MemoryImageSource(height,width, rowFlipPixels(rot90Pixels(pixels, width, height), width, height), 0,height));
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
ImageIcon ica = new ImageIcon(image1);
label.setIcon(ica);
repaint();
}
private int[] rowFlipPixels(int pixels[], int width, int height) {
int newPixels[] = null;
if ((width * height) == pixels.length) {
newPixels = new int[width * height];
int newIndex = 0;
for (int y = height - 1; y >= 0; y--)
for (int x = width - 1; x >= 0; x--)
newPixels[newIndex++] = pixels[y * width + x];
}
return newPixels;
}
private int[] rot90Pixels(int pixels[], int width, int height) {
int newPixels[] = null;
if ((width * height) == pixels.length) {
newPixels = new int[width * height];
int newIndex = 0;
for (int x = width - 1; x >= 0; x--)
for (int y = 0; y < height; y++)
newPixels[newIndex++] = pixels[y * width + x];
}
return newPixels;
}
作者:
曹睿翔
时间:
2013-6-7 12:19
还可以加水印呢!
作者:
yp324
时间:
2013-6-7 12:46
没看代码,我觉得你可以写一个旋转180的方法,在这个方法里面,写一个for循环,调用旋转90度两次。270度就调用三次
作者:
袁梦希
时间:
2013-6-12 18:52
问题都这么高深呢
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2