字符串轉為QRCode

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;

public class QRcodeActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
        	BitMatrix matrix = new MultiFormatWriter().encode("wfung_kwok", BarcodeFormat.QR_CODE, 480, 480);
        	int width = matrix.getWidth();
        	int height = matrix.getHeight();
        	
        	int[] pixels = new int[width * height];
        	for(int y=0;y<height;y++){
        		for(int x = 0; x < width;x++){
        			if(matrix.get(x, y)){
        				pixels[y * width + x] = 0xff000000;
        			}
        		}
        	}
        	
        	Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        	bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        	
        	((ImageView)findViewById(R.id.qrcodeimage)).setImageBitmap(bitmap);
		} catch (Exception e) {
			// TODO: handle exception
		}
    }
}


在我的資源中有要用到的jar包,名為core.jar!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章