java實(shí)現(xiàn)圖片水平和垂直翻轉(zhuǎn)效果
最近在做小型游戲,遇到了要翻轉(zhuǎn)圖片的苦惱,經(jīng)過我一下午的研究,終于發(fā)現(xiàn)了一種好用的方法。
部分代碼來源于別人,我在這個(gè)基礎(chǔ)上修改了下,變得更好用了,之前的別人Image輸入都是BufferedImage,我改成了Image,也就是加了一個(gè)轉(zhuǎn)換。
大家如果看不懂代碼沒關(guān)系,會(huì)用就行了,我會(huì)展示一下怎么用的。
ImageRotate類
package mypackage;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* Author Mythos_Q
*
* Time 2011-07-20
*
* Description 圖片旋轉(zhuǎn)、翻轉(zhuǎn)處理
*/
public class ImgRotate
{
static int Up_Down_Reverse = 0;
static int Left_Right_Reverse = 1;
/**
* 旋轉(zhuǎn)圖片為指定角度
*
* @param bufferedimage
* 目標(biāo)圖像
* @param degree
* 旋轉(zhuǎn)角度
* @return
*/
public static BufferedImage rotateImage(final BufferedImage bufferedimage,final int degree){
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2 + (w>h?(w-h)/2:(h-w)/2));
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return img;
}
/**
* 旋轉(zhuǎn)圖片為指定角度
*
* @param file
* 目標(biāo)圖像
* @param degree
* 旋轉(zhuǎn)角度(90,180,270)
* @return
*/
public static File rotateImage(File file,int degree) throws Exception{
if(degree==90) return rotateImage90(file);
if(degree==180) return rotateImage180(file);
if(degree==270) return rotateImage270(file);
return null;
}
public static Image rotateImage(Image Image,int degree)
{
if(degree==90)
return rotateImage90(Image);
if(degree==180)
return rotateImage180(Image);
if(degree==270)
return rotateImage270(Image);
return null;
}
private static Image rotateImage90(Image image)
{
BufferedImage bufferedimage = ImageToBufferedImage(image);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d =
(img = new BufferedImage(h, w, type) ).createGraphics()
).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w-h)/2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return BufferedImageToImage(img);
}
//左轉(zhuǎn)90度
public static File rotateImage90(File file) throws Exception
{
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d =
(img = new BufferedImage(h, w, type) ).createGraphics()
).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w-h)/2);
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
ImageIO.write(img,"jpg",file);
return file;
}
//右轉(zhuǎn)90度
public static File rotateImage270(File file) throws Exception
{
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(90), w / 2 - (w-h)/2, h / 2 );
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
ImageIO.write(img,"jpg",file);
return file;
}
public static Image rotateImage270(Image image)
{
BufferedImage bufferedimage = ImageToBufferedImage(image);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(90), w / 2 - (w-h)/2, h / 2 );
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return BufferedImageToImage(img);
}
//對(duì)轉(zhuǎn)
public static File rotateImage180(File file) throws Exception
{
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(180), w / 2, h / 2 );
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
ImageIO.write(img,"jpg",file);
return file;
}
public static Image rotateImage180(Image image)
{
BufferedImage bufferedimage = ImageToBufferedImage(image);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int type = bufferedimage.getColorModel().getTransparency();
BufferedImage img;
Graphics2D graphics2d;
(graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
graphics2d.rotate(Math.toRadians(180), w / 2, h / 2 );
graphics2d.drawImage(bufferedimage, 0, 0, null);
graphics2d.dispose();
return BufferedImageToImage(img);
}
/***
* 圖片鏡像處理
* @param file
* @param FX 0 為上下反轉(zhuǎn) 1 為左右反轉(zhuǎn)
* @return
*/
public static void imageMisro(File file,int FX)
{
try
{
BufferedImage bufferedimage = ImageIO.read(file);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int[][] datas = new int[w][h];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
datas[j][i] = bufferedimage.getRGB(j, i);
}
}
int[][] tmps = new int[w][h];
if (FX == 0) {
for (int i = 0, a = h - 1; i < h; i++, a--) {
for (int j = 0; j < w; j++) {
tmps[j][a] = datas[j][i];
}
}
} else if (FX == 1) {
for (int i = 0; i < h; i++) {
for (int j = 0, b = w - 1; j < w; j++, b--) {
tmps[b][i] = datas[j][i];
}
}
}
for (int i = 0; i < h; i++){
for (int j = 0; j<w ;j++){
bufferedimage.setRGB(j, i, tmps[j][i]);
}
}
ImageIO.write(bufferedimage, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
*
* 鏡像處理,輸入image和方式,返回翻轉(zhuǎn)的新image
* type = 0 表示上下翻轉(zhuǎn),type = 1 表示左右翻轉(zhuǎn)
*/
public static Image imageMisro(Image image,int type )
{
try
{
//用到了自己寫的方法
BufferedImage bufferedimage = ImageToBufferedImage(image);
int w = bufferedimage.getWidth();
int h = bufferedimage.getHeight();
int[][] datas = new int[w][h];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
datas[j][i] = bufferedimage.getRGB(j, i);
}
}
int[][] tmps = new int[w][h];
if (type == 0) {
for (int i = 0, a = h - 1; i < h; i++, a--) {
for (int j = 0; j < w; j++) {
tmps[j][a] = datas[j][i];
}
}
} else if (type == 1) {
for (int i = 0; i < h; i++) {
for (int j = 0, b = w - 1; j < w; j++, b--) {
tmps[b][i] = datas[j][i];
}
}
}
for (int i = 0; i < h; i++){
for (int j = 0; j<w ;j++){
bufferedimage.setRGB(j, i, tmps[j][i]);
}
}
Image newImage = (Image)bufferedimage;
return newImage;
//ImageIO.write(bufferedimage, "jpg", file);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//Image轉(zhuǎn)換成BufferedImage
public static BufferedImage ImageToBufferedImage( Image image )
{
BufferedImage bufferedimage = new BufferedImage
(image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedimage.createGraphics();
g.drawImage(image, 0, 0, null); //這里,大家可能會(huì)有疑問,似乎沒有對(duì)bufferedimage干啥
g.dispose(); //但是是正確的,g調(diào)用drawImage就自動(dòng)保存了
return bufferedimage;
}
//BufferedImage 轉(zhuǎn)換成Image類型
public static Image BufferedImageToImage( BufferedImage b )
{
return (Image)b;
}
}
下面展示用法:
package mypackage;
import java.awt.*;
import java.awt.image.*;
import java.net.URL;
import javax.swing.*;
public class Test extends JPanel
{
JFrame frame;
Image image = new ImageIcon("hand2.jpg").getImage();
public void paint( Graphics g )
{
g.drawImage(image, 0, 0,null); //之前的圖片
Image m = ImgRotate.imageMisro(image, ImgRotate.Left_Right_Reverse);
g.drawImage(m, 200, 200, null); //水平翻轉(zhuǎn)的圖片
Image mm = ImgRotate.rotateImage(m, 90); //這里只能填90,180,270
g.drawImage(mm, 250, 0, null);
}
public Test()
{
frame = new JFrame();
frame.add(this);
frame.setSize(500,500);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args)
{
new Test();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
在idea中創(chuàng)建SpringBoot項(xiàng)目
這篇文章主要介紹了在idea中創(chuàng)建SpringBoot項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
java 讀取excel文件轉(zhuǎn)換成json格式的實(shí)例代碼
這篇文章主要介紹了 java 讀取excel文件轉(zhuǎn)換成json格式的實(shí)例代碼,需要的朋友可以參考下2018-04-04
SpringBoot JWT接口驗(yàn)證實(shí)現(xiàn)流程詳細(xì)介紹
這篇文章主要介紹了SpringBoot+JWT實(shí)現(xiàn)接口驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-09-09
Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式)
這篇文章主要介紹了Java中的有限狀態(tài)機(jī)(設(shè)計(jì)模式——狀態(tài)模式),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
springboot如何實(shí)現(xiàn)異步響應(yīng)請(qǐng)求(前端請(qǐng)求超時(shí)的問題解決)
這篇文章主要給大家介紹了關(guān)于springboot如何實(shí)現(xiàn)異步響應(yīng)請(qǐng)求(前端請(qǐng)求超時(shí)的問題解決)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用springboot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-01-01
Java使用過濾器防止SQL注入XSS腳本注入的實(shí)現(xiàn)
這篇文章主要介紹了Java使用過濾器防止SQL注入XSS腳本注入,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
基于Mybatis-Plus攔截器實(shí)現(xiàn)MySQL數(shù)據(jù)加解密的示例代碼
用戶的一些敏感數(shù)據(jù),例如手機(jī)號(hào)、郵箱、身份證等信息,在數(shù)據(jù)庫(kù)以明文存儲(chǔ)時(shí)會(huì)存在數(shù)據(jù)泄露的風(fēng)險(xiǎn),因此需要進(jìn)行加密,解密等功能,接下來本文就給大家介紹基于Mybatis-Plus攔截器實(shí)現(xiàn)MySQL數(shù)據(jù)加解密,需要的朋友可以參考下2023-07-07
Spring基于ProxyFactoryBean創(chuàng)建AOP代理
這篇文章主要介紹了Spring基于ProxyFactoryBean創(chuàng)建AOP代理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
public?static?void?main(String[]?args)使用解讀
這篇文章主要介紹了public?static?void?main(String[]?args)的使用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01

