智者不为愚者谋,勇者不为怯者死。这篇文章主要讲述Android二维码之创建相关的知识,希望能为你提供帮助。
文章图片
1.android 有自带的jar包可以生成二维码core-3.0.0.jar,其中的com.google.zxing包
2.写一个二维码生成的工具类,网上搜的话应该一大堆。
文章图片
文章图片
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.LinearLayout.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 *生成条形码和二维码的工具 27 */ 28 public class ZXingUtils { 29/** 30* 生成二维码 要转换的地址或字符串,可以是中文 31* 32* @param url 33* @param width 34* @param height 35* @return 36*/ 37public static Bitmap createQRImage(String url, final int width, final int height) { 38try { 39// 判断URL合法性 40if (url == null || "".equals(url) || url.length() < 1) { 41return null; 42} 43Hashtable< EncodeHintType, String> hints = new Hashtable< EncodeHintType, String> (); 44hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); 45// 图像数据转换,使用了矩阵转换 46BitMatrix bitMatrix = new QRCodeWriter().encode(url, 47BarcodeFormat.QR_CODE, width, height, hints); 48int[] pixels = new int[width * height]; 49// 下面这里按照二维码的算法,逐个生成二维码的图片, 50// 两个for循环是图片横列扫描的结果 51for (int y = 0; y < height; y++) { 52for (int x = 0; x < width; x++) { 53if (bitMatrix.get(x, y)) { 54pixels[y * width + x] = 0xff000000; 55} else { 56pixels[y * width + x] = 0xffffffff; 57} 58} 59} 60// 生成二维码图片的格式,使用ARGB_8888 61Bitmap bitmap = Bitmap.createBitmap(width, height, 62Bitmap.Config.ARGB_8888); 63bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 64return bitmap; 65} catch (WriterException e) { 66e.printStackTrace(); 67} 68return null; 69} 70 71/** 72* 生成条形码 73* 74* @param context 75* @param contents 76*需要生成的内容 77* @param desiredWidth 78*生成条形码的宽带 79* @param desiredHeight 80*生成条形码的高度 81* @param displayCode 82*是否在条形码下方显示内容 83* @return 84*/ 85public static Bitmap creatBarcode(Context context, String contents, 86int desiredWidth, int desiredHeight, boolean displayCode) { 87Bitmap ruseltBitmap = null; 88/** 89* 图片两端所保留的空白的宽度 90*/ 91int marginW = 20; 92/** 93* 条形码的编码类型 94*/ 95BarcodeFormat barcodeFormat = BarcodeFormat.CODE_128; 96 97if (displayCode) { 98Bitmap barcodeBitmap = encodeAsBitmap(contents, barcodeFormat, 99desiredWidth, desiredHeight); 100Bitmap codeBitmap = creatCodeBitmap(contents, desiredWidth + 2 101* marginW, desiredHeight, context); 102ruseltBitmap = mixtureBitmap(barcodeBitmap, codeBitmap, new PointF( 1030, desiredHeight)); 104} else { 105ruseltBitmap = encodeAsBitmap(contents, barcodeFormat, 106desiredWidth, desiredHeight); 107} 108 109return ruseltBitmap; 110} 111 112/** 113* 生成条形码的Bitmap 114* 115* @param contents 116*需要生成的内容 117* @param format 118*编码格式 119* @param desiredWidth 120* @param desiredHeight 121* @return 122* @throws WriterException 123*/ 124protected static Bitmap encodeAsBitmap(String contents, 125BarcodeFormat format, int desiredWidth, int desiredHeight) { 126final int WHITE = 0xFFFFFFFF; 127final int BLACK = 0xFF000000; 128 129MultiFormatWriter writer = new MultiFormatWriter(); 130BitMatrix result = null; 131try { 132result = writer.encode(contents, format, desiredWidth, 133desiredHeight, null); 134} catch (WriterException e) { 135// TODO Auto-generated catch block 136e.printStackTrace(); 137} 138 139int width = result.getWidth(); 140int height = result.getHeight(); 141int[] pixels = new int[width * height]; 142// All are 0, or black, by default 143for (int y = 0; y < height; y++) { 144int offset = y * width; 145for (int x = 0; x < width; x++) { 146pixels[offset + x] = result.get(x, y) ? BLACK : WHITE; 147} 148} 149 150Bitmap bitmap = Bitmap.createBitmap(width, height, 151Bitmap.Config.ARGB_8888); 152bitmap.setPixels(pixels, 0, width, 0, 0, width, height); 153return bitmap; 154} 155 156/** 157* 生成显示编码的Bitmap 158* 159* @param contents 160* @param width 161* @param height 162* @param context 163* @return 164*/ 165protected static Bitmap creatCodeBitmap(String contents, int width, 166int height, Context context) { 167TextView tv = new TextView(context); 168LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( 169LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 170tv.setLayoutParams(layoutParams); 171tv.setText(contents); 172tv.setHeight(height); 173tv.setGravity(Gravity.CENTER_HORIZONTAL); 174tv.setWidth(width); 175tv.setDrawingCacheEnabled(true); 176tv.setTextColor(Color.BLACK); 177tv.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 178MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); 179tv.layout(0, 0, tv.getMeasuredWidth(), tv.getMeasuredHeight()); 180 181tv.buildDrawingCache(); 182Bitmap bitmapCode = tv.getDrawingCache(); 183return bitmapCode; 184} 185 186/** 187* 将两个Bitmap合并成一个 188* 189* @param first 190* @param second 191* @param fromPoint 192*第二个Bitmap开始绘制的起始位置(相对于第一个Bitmap) 193* @return 194*/ 195protected static Bitmap mixtureBitmap(Bitmap first, Bitmap second, 196PointF fromPoint) { 197if (first == null || second == null || fromPoint == null) { 198return null; 199} 200int marginW = 20; 201Bitmap newBitmap = Bitmap.createBitmap( 202first.getWidth() + second.getWidth() + marginW, 203first.getHeight() + second.getHeight(), Config.ARGB_4444); 204Canvas cv = new Canvas(newBitmap); 205cv.drawBitmap(first, marginW, 0, null); 206cv.drawBitmap(second, fromPoint.x, fromPoint.y, null); 207cv.save(Canvas.ALL_SAVE_FLAG); 208cv.restore(); 209 210return newBitmap; 211} 212 213 }
ZXingUtils3.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);
例如:
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);
【Android二维码之创建】
推荐阅读
- Android开发BUG及解决方法2
- Android 之使用LocalBroadcastManager解决BroadcastReceiver安全问题
- android导入项目常见问题解决
- Android数据传递全局变量的使用方法
- Android开发之方向传感器的使用? (2011-10-13 20:56:05)转载▼
- java中的+与append函数
- 高德地图-android 权限设置
- Spring中BeanFactory和ApplicationContext的区别
- eclipse项目导入到android studio中文乱码处理