[Java]檔案整理工具-照片整理

有時照片從相機複製下來後懶的整理,時間一久就越來越不好查看到底是在何時拍的,這時候就可以利用這個程式來做分類整理,主要是利用遞迴的方法一直向指定目錄下的每一層找尋圖片檔,再複製至以年月為目錄的分類資料夾。

程式的作法大概是先取得目錄列表,再判斷每一個檔案是目錄或是檔案,如果是檔案,檢查是否為jpg檔,如果是檔案,則取得其修改日期的年月,建立存放目錄的年月目錄資料夾,並複製檔案至此資料夾,如果是目錄,則迴丟至此function,製作遞迴效果。

ImgFilter.png

Read More

將文字加入圖片裡

如果有時需要把文字加到圖片裡,像是加入亂數碼、流水號等,要怎麼做呢?

方法很簡單,只需要先把input 檔案打開,再取得其畫筆,在我們要寫入文字的x y座標上,利用drawString把文字印上去就可以了。

首先我們先找一張圖,如果找不到的人可以利用以下這張圖,然後會在(150,113)印上"土地公"三個大字,顏色是紅色的、標楷體size 24。

1.jpg
Read More

[jsp小技巧]利用Throw Exception來完成Ajax

一般在寫Ajax時,最直覺的使用方法就是利用Ajax Object來要求Server給與回應, 再依回應的內容解西倒底是完成動作?還是有錯誤產生,如必填欄位未填。

這裡介紹一個小技巧,在編譯式的網頁伺服器語言可能比較合適使用。

大概的原理是利用程式在執行有錯誤時Throw Exception來當做錯誤訊息, 回傳給client,而client只要接收到503的錯誤,則就可以知道動作並未完成, 反之則是完成。

範例使用prototype來使用Ajax,傳送及回應,有需要可以參考:

Ajax 使用prototype.js 1

以下是載行結果,當有填值時,則回應填寫的值,

沒填值的時候就回傳錯誤訊息。(Exception)

1
2
Read More

Java利用Imagick來ReSize圖片檔-使用JMagick

程式很簡單 ,只需要在利用command在程式目錄下執行

java -jar ReSizeImage.jar

就可以啦!

預設是縮成以寬為500px基準,如果要改變寬可以利用

java -jar ReSizeImage.jar 數字

縮完的圖會放到程式目錄的resize目錄裡

我Blog都是以500寬為基準,我想應該夠用了吧^^

程式目錄結構

  • ReSizeImage.jar
  • jmagick.dll
  • lib/Jmagick.jar

記得要先下載Imagick來安裝才能使用喔

按裝方法如下:

JMagick-Java open source free影像壓縮

範例圖檔可以按圖下載,放置於程式目錄

DSCN3599.JPG

原始碼下載

class Tool整個壓縮以外的實作均在此

package image;

import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class Tool {

    public Tool(int width) {

        // 取得class路徑所有檔案
        File f = new File(".");
        File[] list = f.listFiles();
        for (int i = 0; i < list.length; i++) {
            // 是否為圖檔,是的話進行縮圖
            if (isImg(getFileType(list[i].getName()))) {
                System.out.println("處理檔案名稱:" + list[i].getName());
                toCompressImg(list[i].getAbsolutePath(), getRealDir(list[i]
                        .getAbsolutePath())
                        + "resize" + File.separator + list[i].getName(), width);
            }
        }

    }

    // 取得副檔名
    private String getFileType(String str) {
        int startIndex = str.lastIndexOf(46) + 1;
        int endIndex = str.length();
        return str.substring(startIndex, endIndex); // 副檔名

    }

    // 傳入副檔名 檢查是否為指定圖檔jpg gif png
    private boolean isImg(String str) {
        return (("jpg,gif,png").indexOf(str.toLowerCase())) > -1;
    }

    // 找目錄
    private String getRealDir(String str) {
        int endIndex = str.lastIndexOf(File.separator) + 1;
        int startIndex = 0;
        return str.substring(startIndex, endIndex); // 副檔名
    }

    public void toCompressImg(String iFile, String oFile, int newWidth) {
        try {
            File fi = new File(iFile); // 大圖文件
            if (!fi.exists())
                return;
            String oPath = oFile
                    .substring(0, oFile.lastIndexOf(File.separator));
            File foPath = new File(oPath);
            if (!foPath.exists())
                if (foPath.mkdir())
                    System.out.println("ooo toCompressImg() File out path:"
                            + oPath + " mkdir成功!");
            File fo = new File(oFile); // 將要轉換出的小圖文件

            int nw = newWidth;
            // AffineTransform transform = new AffineTransform();
            BufferedImage bis = ImageIO.read(fi);
            int w = 0, h = 0;
            try {
                w = bis.getWidth();
                h = bis.getHeight();
            } catch (Exception e) {
                fileCopy(fi, fo);
                return;
            }
            if (nw > (w < h ? h : w)) {
                fileCopy(fi, fo);
                return;
            } // 壓縮寬度比原始檔大,那就不壓縮了;

            int nh = (nw * h) / w;
            // double sx = (double) nw / w;
            // double sy = (double) nh / h;

            JMagickScale.Compress(fi.getAbsolutePath(), fo.getAbsolutePath(),
                    nw, nh);
            /*
             * transform.setToScale(sx, sy); //System.out.println(w + ", " + h);
             * AffineTransformOp ato = new AffineTransformOp(transform, null);
             * BufferedImage bid = new BufferedImage(nw, nh,
             * BufferedImage.TYPE_3BYTE_BGR); ato.filter(bis, bid);
             * ImageIO.write(bid, "jpeg", fo);
             */
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void fileCopy(File inputFile, File outputFile) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(inputFile);
            fos = new FileOutputStream(outputFile);
            int c;
            while ((c = fis.read()) != -1)
                fos.write(c);

            fis.close();
            fos.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void main(String args[]) {
        int width = 500;

        try {
            width = Integer.parseInt(args[0]);
        } catch (Exception e) {

        }
        if (width <= 0) {
            System.out.println("Width Error");
        } else {
            new Tool(width);
        }

    }
}

class JmagickScale壓縮的實作

package image;

import magick.ImageInfo;
import magick.MagickException;
import magick.MagickImage;

public class JMagickScale {
    public static void Compress(String source, String To, int width, int height)
            throws MagickException {
        if (System.getProperty("jmagick.systemclassloader") == null) {
            System.setProperty("jmagick.systemclassloader", "no");
        }
        ImageInfo info = new ImageInfo(source);
        MagickImage image = new MagickImage(info);

        // resize image

        MagickImage scaleImg = image.scaleImage(width, height);

        // write image to file
        scaleImg.setFileName(To);
        scaleImg.writeImage(info);

    }
}

成功的圖,可以按進去flickr看壓好的圖效果

resizeDSCN3599.JPG