posts - 3,  comments - 2,  trackbacks - 0
            2010年2月9日
          private void compressImageFile() {
                  JFileChooser fileChooser = new JFileChooser("d://");
                  fileChooser.showOpenDialog(this);
                  File file = fileChooser.getSelectedFile();
          
                  if (null != file && file.exists()) {
                      Toolkit toolkit = Toolkit.getDefaultToolkit();
                      Image srcImage = toolkit.getImage(file.getAbsolutePath()); // 構(gòu)造Image對象
                      int wideth = -1;
                      int height = -1;
                      boolean flag = true;
                      while (flag) {
                          wideth = srcImage.getWidth(null); // 得到源圖寬
                          height = srcImage.getHeight(null); // 得到源圖長
                          System.out.println("wideth:" + wideth + " height:" + height);
                          if (wideth > 0 && height > 0) { // 因為 Toolkit.getImage 是異步讀取,如果
                                                          // wideth 和 height
                                                          // 都大于0,表明圖片已經(jīng)加載完畢
                          // imageCanvas.setImage(srcImage);
                              flag = false;
                          } else {
                              try {
                                  Thread.sleep(10);
                              } catch (Exception e) {
                                  e.printStackTrace();
                              }
                          }
                      }
          
                      // 加載完畢,輸出
                      int w = 1024;
                      float s = (float) wideth / 1024.0f;
                      int h = (int) ((float) height / s);
                      BufferedImage bufferedImage = new BufferedImage(w, h,
                              BufferedImage.TYPE_INT_RGB);
                      // bufferedImage.getGraphics().drawImage(srcImage, 0, 0, 1024,
                      // 768, null); // 繪制縮小后的圖
                      boolean flag2 = false;
                      while (!(flag2 = bufferedImage.getGraphics().drawImage(srcImage, 0,
                              0, w, h, this))) {
                          try {
                              Thread.sleep(10);
                          } catch (Exception e) {
                              e.printStackTrace();
                          }
                      }
          
                      try {
          
                          File outputFile = new File("d://hhh.jpg");
                          if (!outputFile.exists()) {
                              outputFile.createNewFile();
                          }
                          FileOutputStream out = new FileOutputStream(outputFile); // 輸出到文件流
                          JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
                          encoder.encode(bufferedImage); // 近JPEG編碼
                          out.close();
                      } catch (Exception e) {
                          e.printStackTrace();
                      }
                  }
              }
          posted @ 2010-02-09 15:37 Mr. Michael.Q 閱讀(216) | 評論 (0)編輯 收藏
          主站蜘蛛池模板: 汨罗市| 杂多县| 贡山| 洞头县| 南昌县| 洛隆县| 上栗县| 化州市| 米泉市| 盐池县| 天峻县| 凤阳县| 灵璧县| 淅川县| 桐城市| 元氏县| 正蓝旗| 静宁县| 卓尼县| 金堂县| 东台市| 墨竹工卡县| 吉首市| 故城县| 毕节市| 讷河市| 珠海市| 新疆| 从化市| 浦东新区| 唐海县| 奉化市| 绥宁县| 丹寨县| 天津市| 靖江市| 大石桥市| 唐河县| 金门县| 广德县| 义乌市|