Browsed by
Category: Java

[Java] Encode UTF-8 characters to Qrcode by Zxing project

[Java] Encode UTF-8 characters to Qrcode by Zxing project

Zxing Project is a famous open-source, multi-format 1D/2D barcode image processing library implemented in Java, with ports to other languages. But I believe there are somebody have the same problem just like me: I can’t Encode UTF-8 characters to Qrcode.

Last time I used Google Chart API to solve this problem, finally I find the way out to use the Zxing project that encode UTF-8 characters to Qrcode correctly.

Read More Read More

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] Binary search algorithm

[Java] Binary search algorithm

Binary search algorithm time complexity is O(log2(n)), and this algorithm requires that source array is sorted in order to work correct.

Algorithm is quite simple. It can be done either recursively or iteratively:

1.get the middle element;
2.if the middle element equals to the searched value, the algorithm stops;
3.otherwise, two cases are possible:
   (1)searched value is less, than the middle element. In this case, go to the step 1 for the part of the array, before middle element.
   (2)searched value is greater, than the middle element. In this case, go to the step 1 for the part of the array, after middle element.

Read More Read More

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] 如何將字串用SHA-1雜湊演算法加密?

[Java] 如何將字串用SHA-1雜湊演算法加密?

為了要將從手機的IMEI加密接著傳入軟體,寫了以下方法,而以下的方法可以加密任何字串(只要你有需求的話),不過這裡是從取得手機的deviceID開始寫起。

首先在activity的onCreat()方法中取得deviceID

TelephonyManager info = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);

String tempDeviceID = info.getDeviceId();

接著將取得的deviceID用SHA-1雜湊演算法重新編碼。

MessageDigest md = MessageDigest.getInstance("SHA1");
md.reset();
byte[] buffer = tempDeviceID.getBytes();
md.update(buffer);
byte[] digest = md.digest();

String deviceID = "";
for (int i = 0; i < digest.length; i++) {
    deviceID += Integer.toString((digest[i] & 0xff) + 0x100, 16).substring(1);
}

取得的deviceID即為重新加密編碼過的字串。

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] 字串正規表示式、字串分割與資料夾建立

[Java] 字串正規表示式、字串分割與資料夾建立

假設從資料庫取出的一個字串物件為:

String picPath = "aaa\bbb\ccc\photo1.png";//將"\"替換為"/"後為網路上某一個圖片檔案的相對路徑

請問要如何把字串中的”\”全部更換成”/”以利後續程式的撰寫呢?

通常大家第一個想到的程式寫法是:

String picPath2 = picPath.replaceAll("\","/");

但是這樣的寫法在Eclipse中編譯並不會通過,找了資料才想起是java正規表示式的緣故;大家猜猜到底要寫幾個”\”和”/”才正確?
正確的寫法為:

String picPath2 = picPath.replaceAll("\\\\","/");

Read More Read More

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] 雙重迴圈 , Java.Math.floor and NumberFormat

[Java] 雙重迴圈 , Java.Math.floor and NumberFormat

事情是這樣的。我需要從一個ArrayList取出字串,將字串轉成Integer型別之後,以300為一個區間分別加入map裡面。數字範圍為0~3300,而我因為趕著要做出畫面給老闆看,也沒有去多加思考怎樣的寫法比較好,便很直覺的寫出以下程式:

for(int i = 0 ; i < list.size(); i++){
    Exhibitor exhibitor = list.get(i);
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("_id", exhibitor.getPK_ID().toString());
    map.put("name", exhibitor.getName());
    map.put("booth", exhibitor.getBoothId());

    if(Integer.valueOf(exhibitor.getBoothId()) < 301){
        map.put("separator", "1~300");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 300 && Integer.valueOf(exhibitor.getBoothId()) < 601){
        map.put("separator", "301~600");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 600 && Integer.valueOf(exhibitor.getBoothId()) < 901){
        map.put("separator", "601~900");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 900 && Integer.valueOf(exhibitor.getBoothId()) < 1201){
        map.put("separator", "901~1200");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 1200 && Integer.valueOf(exhibitor.getBoothId()) < 1501){
        map.put("separator", "1201~1500");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 1500 && Integer.valueOf(exhibitor.getBoothId()) < 1801){
        map.put("separator", "1501~1800");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 1800 && Integer.valueOf(exhibitor.getBoothId()) < 2101){
        map.put("separator", "1801~2100");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 2100 && Integer.valueOf(exhibitor.getBoothId()) < 2401){
        map.put("separator", "2101~2400");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 2400 && Integer.valueOf(exhibitor.getBoothId()) < 2701){
        map.put("separator", "2401~2700");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 2700 && Integer.valueOf(exhibitor.getBoothId()) < 3001){
        map.put("separator", "2701~3000");
    }else if(Integer.valueOf(exhibitor.getBoothId()) > 3000 && Integer.valueOf(exhibitor.getBoothId()) < 3301){
        map.put("separator", "3001~3300");
    }
    items.add(map);
}

Read More Read More

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] 將小寫字串轉為大寫並取出字首的方法

[Java] 將小寫字串轉為大寫並取出字首的方法


//假設我有一個字串:
String a = "howareyou";

//需要將其全部轉為大寫:
String upperCaseA = a.toUpperCase(); //此時a = "HOWAREYOU"

//轉換成大寫之後取出該字串的字首:
Char firstWord = upperCaseA.charAt(0); //此時 firstWord = "H"

//但是因為firstWord物件的形態為char,需轉為String 之後才能使用:
String stringFirstWord = Character.toString(firstWord);

以上的程式其實用兩行就夠了:

String a = "howareyou";
String firstWord = Character.toString(a.toUpperCase().charAt(0));

以上,收工。

Facebooktwittergoogle_plusredditpinterestlinkedinmail
[Java] Java Simple Date Format 速查表(for SimpleDateFormat)

[Java] Java Simple Date Format 速查表(for SimpleDateFormat)

假設我要把今天的日期轉換成以下其中一個格式該如何做呢?

首先在程式碼裡面先用Calendar取得現在的時間與日期:

Calendar today = Calendar.getInstance();

但是取得的today格式很難閱讀,所以再轉換成date格式:

Date date = today.getTime();

接著來做時間格式的轉換:

DateFormat df = new SimpleDateFormat("yyyy-MM-dd");//可將括號內的字串換成以下格式
String dateFormat = df.format(date);

最後即可取得需要的時間格式囉!

Read More Read More

Facebooktwittergoogle_plusredditpinterestlinkedinmail