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

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

          導航

          <2006年3月>
          2627281234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          統計

          公告

          個人寫的作品會盡量附上代碼,大家使用發現問題就指出,交流第一嘛  QQ:316228067

          常用鏈接

          留言簿(13)

          隨筆分類(121)

          隨筆檔案(105)

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          [原創]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 閱讀(2077) 評論(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());圖片的長寬參數設置反了  回復  更多評論   

          主站蜘蛛池模板: 洱源县| 固原市| 河间市| 雷州市| 金川县| 西安市| 邛崃市| 北辰区| 漳浦县| 巫溪县| 微山县| 镇康县| 巨鹿县| 盐城市| 东方市| 博湖县| 合肥市| 常熟市| 安吉县| 铜陵市| 漳浦县| 巴林右旗| 宾阳县| 衡阳县| 朝阳县| 鄂托克旗| 福鼎市| 酉阳| 高雄市| 八宿县| 安化县| 和平区| 稻城县| 绥芬河市| 理塘县| 怀柔区| 临海市| 通城县| 沐川县| 隆林| 汤原县|