真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

java實現(xiàn)圖片水平和垂直翻轉(zhuǎn)效果

最近在做小型游戲,遇到了要翻轉(zhuǎn)圖片的苦惱,經(jīng)過我一下午的研究,終于發(fā)現(xiàn)了一種好用的方法。

網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了雁江免費建站歡迎大家使用!

部分代碼來源于別人,我在這個基礎上修改了下,變得更好用了,之前的別人Image輸入都是BufferedImage,我改成了Image,也就是加了一個轉(zhuǎn)換。

大家如果看不懂代碼沒關系,會用就行了,我會展示一下怎么用的。

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
  *   目標圖像
  * @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
  *   目標圖像
  * @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);
 }
 //對轉(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

下面展示用法: 

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();
 
 }
 
 
}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


網(wǎng)站名稱:java實現(xiàn)圖片水平和垂直翻轉(zhuǎn)效果
網(wǎng)頁URL:http://weahome.cn/article/pssspg.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部