posts - 3,  comments - 2,  trackbacks - 0
          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()); // 構造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,表明圖片已經加載完畢
                          // 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 on 2010-02-09 15:37 Mr. Michael.Q 閱讀(216) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 临城县| 万源市| 嵊泗县| 旬邑县| 任丘市| 龙川县| 西乌珠穆沁旗| 吉林省| 德令哈市| 三门峡市| 镇宁| 尚志市| 宁都县| 都匀市| 盈江县| 祁连县| 洪洞县| 普陀区| 裕民县| 通渭县| 江孜县| 商丘市| 长岭县| 乐亭县| 惠水县| 新宁县| 溧阳市| 刚察县| 方正县| 凭祥市| 乌拉特后旗| 新闻| 金寨县| 晋宁县| 宜黄县| 祁东县| 治多县| 子洲县| 青海省| 赤水市| 紫阳县|