1、問題描述 |
在開發中須要將信息轉換爲二維碼存儲並要求帶有公司的logo,咱們知道Google的Zxing開源項目就很好的幫助咱們實現條形碼、二維碼的生成和解析,但帶有logo的官網並無提供demo,下面就經過實例看看如何實現以及Zxing的使用。html
2、案例介紹 |
在項目中加入jar,只需加入core.jargit
Zxing項目地址:https://github.com/zxing/zxing/github
3、Zxing主要組件 |
定義了不一樣的二進制編碼方式,取值以下web
EAN_13條形碼,共計13位代碼,比較常見,如商品上的包裝上的都是這種條形碼數組
CODE_QR二維碼(矩陣碼),比條形碼存在更多信息,當下比較流行ide
CODE_128條形碼 可表示可表示從 ASCII 0 到ASCII 127 共128個字符,用於企業管理,生產流程控制編碼
CODE_39條形碼,編制簡單隻接受以下43個字符spa
主要包含一個 encode()方法,可實現產生編碼(條形、二維碼)code
BitMatrix encode(String contents, BarcodeFormat format, int width, int height,Hashtable hints)方法orm
參數:
contents:要編碼的內容
format:編碼格式(條形、二維)
width,height:生成碼的大小
hints:包含EncodeHintType(編碼提示類型)信息的集合,主要設置字符編碼,好比支持漢字的utf-8,以下:
Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>();
hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");
返回值:BitMatrix 二維矩陣點
BitMatrix :表現爲一個二維矩陣,x表示列的位置,y表示行的位置,循序從左上角開始,一列一列排列(先x後y)
主要方法:
getWidth():返回矩陣的寬度
getHeight():返回矩陣的高度
boolean get(x,y) :很是重要的方法,實現根據給定的x,y判斷該位置是否有黑塊
在產生二維碼的應用中就是經過這個方法進行判斷,而後把有黑塊的點記錄下來,使用Bitmap的setPixels()方法生成圖形,詳解案例的createCode()方法中的代碼
4、完整代碼 |
public class MainActivity extends Activity { private EditText etCompany; private EditText etPhone; private EditText etEmail; private EditText etWeb; private Bitmap logo; private static final int IMAGE_HALFWIDTH = 40;//寬度值,影響中間圖片大小 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //得到資源圖片,可改爲獲取本地圖片或拍照獲取圖片 logo=BitmapFactory.decodeResource(super.getResources(),R.drawable.y014); etCompany =(EditText) findViewById(R.id.etCompany); etPhone=(EditText) findViewById(R.id.etPhone); etEmail =(EditText) findViewById(R.id.etEmail); etWeb =(EditText) findViewById(R.id.etWeb); findViewById(R.id.but).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub String company=etCompany.getText().toString().trim() ; String phone =etPhone .getText().toString().trim() ; String email = etEmail.getText().toString().trim() ; String web = etWeb.getText().toString().trim() ; //二維碼中包含的文本信息 String contents= "BEGIN:VCARD\nVERSION:3.0\nORG:"+company+"\nTEL:"+phone+"\nURL:"+web+"\nEMAIL:"+email+"\nEND:VCARD"; try { //調用方法createCode生成二維碼 Bitmap bm=createCode(contents,logo,BarcodeFormat.QR_CODE); ImageView img=(ImageView)findViewById(R.id.imgCode) ; //將二維碼在界面中顯示 img.setImageBitmap(bm); } catch (WriterException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); } /** * 生成二維碼 * @param string 二維碼中包含的文本信息 * @param mBitmap logo圖片 * @param format 編碼格式 * @return Bitmap 位圖 * @throws WriterException */ public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format) throws WriterException { Matrix m = new Matrix(); float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth(); float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight(); m.setScale(sx, sy);//設置縮放信息 //將logo圖片按martix設置的信息縮放 mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), m, false); MultiFormatWriter writer = new MultiFormatWriter(); Hashtable<EncodeHintType, String> hst = new Hashtable<EncodeHintType, String>(); hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//設置字符編碼 BitMatrix matrix = writer.encode(string, format, 400, 400, hst);//生成二維碼矩陣信息 int width = matrix.getWidth();//矩陣高度 int height = matrix.getHeight();//矩陣寬度 int halfW = width / 2; int halfH = height / 2; int[] pixels = new int[width * height];//定義數組長度爲矩陣高度*矩陣寬度,用於記錄矩陣中像素信息 for (int y = 0; y < height; y++) {//從行開始迭代矩陣 for (int x = 0; x < width; x++) {//迭代列 if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {//該位置用於存放圖片信息 //記錄圖片每一個像素信息 pixels[y * width + x] = mBitmap.getPixel(x - halfW + IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); } else { if (matrix.get(x, y)) {//若是有黑塊點,記錄信息 pixels[y * width + x] = 0xff000000;//記錄黑塊信息 } } } } Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); // 經過像素數組生成bitmap bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } }
另,若是須要將將產生的二維碼保存到磁盤中,可參考博文http://www.cnblogs.com/jerehedu/p/4464870.html中BitmapUtil的saveBmpToSd()方法
想要進一步瞭解的同窗,能夠點擊查看源代碼,親自運行體驗!