[Java]如何產生Qrocde二維條碼?

QRCode二維條碼,是Quick Response Code縮寫,來自日本的國際二維條碼標準,是在1994年由日本Denso-Wave公司所發明的,此公司並保有版權但並不行使版權,所以在使用QRCode上並沒有版權上的問題。QRCode的應用像是購物消費、資料查詢、名片交換等等,在日本使用上是十分常見的,而台灣在近幾年也開始流行了起來。

更詳細的QRCode介紹可以參考Wiki QR碼或是 行動條碼 / 二維碼 / Mobile Barcode / QRCode

要利用Java來產生QRCode可以使用swetake所開放出來的原始碼http://www.swetake.com/qr/index-e.html,有php、ruby及Java等版本,下載點qrcode_java0.50beta10.tar.gz ,下載後解壓縮,可以直接把lib目錄下的QRcode.jar直接引用到程式classpath裡就可以了,或著把所有src目錄裡的java檔copy到專案source目錄裡也可。

qrcode壓縮檔

Eclipse的配置如下圖

Eclipse內的配置圖

Read More

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

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

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

ImgFilter.png

Read More

[Java]簡單的Socket Client Server

這是一個超簡單的Client Server範例,Server部份利用Thread來常駐執行,而Client利用Socket去連接指定的port,送出一個字元組給OutputStream,而Server接收到請求後,使用InputStream來取得資料。

這裡有篇介紹Socket Server的原理Thread應用-Java SocketServer,還有一篇傳送物件Serializable序列化Socket傳送範例,這篇算是比較進階。

程式碼包裝在jar裡,利用二個command視窗,一個執行Server一個執行Client,結果如下:

Java Socket Client Server

原始碼及jar下載:


點我下載

Read More

網頁post自動產生iframe

大部份在寫網頁要上傳檔案時都需要使用到iframe做一個中繼,可是如果上傳POST時有錯訊息時,前一個iframe資料就會變成記錄history,而此時如果使用者重新整理時,就會遇到暫存資料的問題,iframe裡的內容會再被執行一次。

以下有一個JavaScript方法,可以在檢查完所有form條件時才自動建立iframe內容createiFrame做post動作,而重新整理時(或第一次進入此畫面時)又不會真實存在,來解決暫存的問題。

當呼叫createIFrame時,會在body這一個tab建立一個iframe元素,並把需求屬性設定好,像是frameborder=0、width=0、height=0等。

function createIFrame() {
        if (!document.getElementById("_hiddenframe"))
        {
            var frame = document.createElement("iframe");
            frame.setAttribute("name", "_hiddenframe1");
            frame.setAttribute("src", "about:blank");
            frame.setAttribute("frameborder", "0");
            frame.setAttribute("height", "0");
            frame.setAttribute("width", "0");
            frame.setAttribute("id", "_hiddenframe");
            frame.name = "_hiddenframe";
            document.body.appendChild(frame);
            window.frames._hiddenframe.name = "_hiddenframe";
            document.getElementsByTagName("body")[0].appendChild(frame);

        }

    }

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