Android QR code creation, android
1. Android has built-in jar package can generate QR code core-3.0.0.jar, where com. google. zxing package
2. Write a tool class generated by the QR code. There should be a lot of online searches.
1 package com. example. administrator. twocodedemo; 2 3 import android. content. context; 4 import android. graphics. bitmap; 5 import android. graphics. bitmap. config; 6 import android. graphics. canvas; 7 import android. graphics. color; 8 import android. graphics. pointF; 9 import android. view. gravity; 10 import android. view. view. measureSpec; 11 import android. widget. linearLayout; 12 import android. widget. line ArLayout. layoutParams; 13 import android. widget. textView; 14 15 import com. google. zxing. barcodeFormat; 16 import com. google. zxing. encodeHintType; 17 import com. google. zxing. multiFormatWriter; 18 import com. google. zxing. writerException; 19 import com. google. zxing. common. bitMatrix; 20 import com. google. zxing. qrcode. QRCodeWriter; 21 22 import java. util. hashtable; 23 24/** 25*26 * tool for generating barcode and QR code 27*/28 public class ZXingUtils {29/*** 30 * generate the address or string to be converted by the QR code, it can be Chinese 31*32 * @ param url 33 * @ param width 34 * @ param height 35 * @ return 36 */37 public static Bitmap createQRImage (String url, final int width, final int height) {38 try {39 // judge URL validity 40 if (url = null | "". equals (url) | url. length () <1) {41 return null; 42} 43 Hashtable <EncodeHintType, String> hints = new Hashtable <Encode HintType, String> (); 44 hints. put (EncodeHintType. CHARACTER_SET, "UTF-8"); 45 // image data conversion, using matrix conversion 46 BitMatrix bitMatrix = new QRCodeWriter (). encode (url, 47 BarcodeFormat. QR_CODE, width, height, hints); 48 int [] pixels = new int [width * height]; 49 // The following uses the QR code algorithm to generate images of QR codes one by one, 50 // two for loops are the results of the horizontal column scan of the image 51 for (int y = 0; y ZXingUtils
3. MainActivity
@OnClick({R.id.btn_create, R.id.iv_two_code}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_create: String url = etUrl.getText().toString().trim(); Bitmap bitmap = ZXingUtils.createQRImage(url, ivTwoCode.getWidth(), ivTwoCode.getHeight()); ivTwoCode.setImageBitmap(bitmap);
For example:
String company = etCompany. getText (). toString (). trim (); String phone = etPhone. getText (). toString (). trim (); String email = etEmail. getText (). toString (). trim (); String web = etWeb. getText (). toString (). trim (); // text information contained in the QR code String contents = "BEGIN: VCARD \ nVERSION: 3.0 \ nORG:" + company + "\ nTEL: "+ phone +" \ nURL: "+ web +" \ nEMAIL: "+ email +" \ nEND: VCARD "; try {// call the createCode method to generate the QR code Bitmap bm = createCode (contents, logo, BarcodeFormat. QR_CODE );