sunfruit[請訪問http://www.fruitres.cn]

          --我相信JAVA能走得更遠 QQ:316228067

          [原創]JAVA在已有圖片上面畫圖的實例

          --sunfruit

          簡介:JAVA在已有圖片上面畫圖的實例,下面的程序在已有的圖片上面畫了一個藍色的方塊

          import java.awt.Color;
          import java.awt.Graphics;
          import java.awt.Graphics2D;
          import java.awt.image.BufferedImage;
          import java.io.FileInputStream;
          import javax.imageio.ImageIO;
          import javax.swing.ImageIcon;
          import java.util.Random;
          import java.io.IOException;
          import java.io.File;
           
          public class ImageTest {
            public ImageTest() throws Exception {
              String ext="png";
              FileInputStream in = new FileInputStream("已有圖片的路徑");
              byte[] bytes = new byte[in.available()];
              in.read(bytes);
              in.close();
              Random random=new Random(System.currentTimeMillis());
             
              ImageIcon imageIcon = new ImageIcon(bytes);
             
              BufferedImage bufferedImage=new BufferedImage(imageIcon.getIconHeight(),imageIcon.getIconWidth(),BufferedImage.TYPE_INT_RGB);
              Graphics2D g=(Graphics2D)bufferedImage.getGraphics();
              g.setColor(Color.blue);
              g.drawRect(5,5,5,5);
              g.fillRect(5,5,5,5);
              g.drawImage(imageIcon.getImage(),0,0,imageIcon.getIconHeight(),imageIcon.getIconWidth(),imageIcon.getImageObserver());
           
              String filepath = System.getProperty("java.io.tmpdir") + random.nextInt(99999) + "." + ext;
              try {
                ImageIO.write(bufferedImage, ext,  new File(filepath));
                System.out.println("文件已經生成,路經為" + filepath);
              }
              catch (IOException ex) {
                ex.printStackTrace();
              }
           

            }
           
            /**
             * @param args
             */
            public static void main(String[] args) throws Exception {
              new ImageTest();
            }
           
          }

          posted on 2006-03-11 12:39 sunfruit 閱讀(2076) 評論(1)  編輯  收藏 所屬分類: JAVA SE & EE

          評論

          # re: [原創]JAVA在已有圖片上面畫圖的實例[未登錄] 2015-02-02 14:10 Allen

          很感謝你的代碼,幫了我大忙
          指出你的兩點遺漏
          1:public ImageTest() throws Exception {沒有寫方法的返回值
          2:g.drawImage(imageIcon.getImage(),0,0,imageIcon.getIconHeight(),imageIcon.getIconWidth(),imageIcon.getImageObserver());圖片的長寬參數設置反了  回復  更多評論   

          主站蜘蛛池模板: 莆田市| 宁化县| 浏阳市| 顺昌县| 桓台县| 平潭县| 沧源| 武功县| 宝山区| 渑池县| 塘沽区| 巩义市| 永川市| 承德县| 中山市| 伊川县| 宜昌市| 黑水县| 会宁县| 环江| 华安县| 内黄县| 凉城县| 抚远县| 吐鲁番市| 盐山县| 铜梁县| 淮北市| 香格里拉县| 寻甸| 应城市| 稷山县| 乃东县| 长乐市| 乐都县| 吴江市| 会理县| 拉孜县| 承德市| 桐庐县| 梁山县|