Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

基於 Android 打造個性彩色二維碼的完整實作

編輯:關於 Android 程式設計

按照我寫程式的風格,先給大家展示下效果圖,各位看過之後要是覺得不錯、挺滿意的話,就請繼續往下閱讀。


之前我也寫過用安卓生成彩色二維碼、帶 logo 的二維碼,也了解可以採用 QRCode 與 ZXing 兩種方式。而這一篇同樣是寫二維碼,使用的則是 BarcodeFormat.QR_CODE。主要是因為看見了太多非常漂亮的二維碼,這裡主要模仿 QQ 的二維碼,並且也高仿實現了長按發送給朋友與保存到圖庫的功能。覺得不錯的就請多多支持,哪裡不好也歡迎提出來。好了,我們一步一步來。

第一步:簡單二維碼實現

先來個最簡單的二維碼:

 

看下簡單程式碼實現:

 /** * 根據指定內容生成自定義寬高的二維碼圖片 * * @param content * 需要生成二維碼的內容 * @param width * 二維碼寬度 * @param height * 二維碼高度 * @throws WriterException * 生成二維碼異常 */ public static Bitmap makeQRImage(String content, int width, int height) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 圖像數據轉換,使用了矩陣轉換 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; // 按照二維碼的算法,逐個生成二維碼的圖片,兩個for循環是圖片橫列掃描的結果 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (bitMatrix.get(x, y))//范圍內為黑色的 pixels[y * width + x] = 0xff000000; else//其他的地方為白色 pixels[y * width + x] = 0xffffffff; } } // 生成二維碼圖片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); //設置像素矩陣的范圍 bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; }

第二步:簡單二維碼加 logo

接下來給二維碼加 logo:(看圖)

 /** * 根據指定內容生成自定義寬高的二維碼圖片 * * param logoBm * logo圖標 * param content * 需要生成二維碼的內容 * param width * 二維碼寬度 * param height * 二維碼高度 * throws WriterException * 生成二維碼異常 */ public static Bitmap makeQRImage(Bitmap logoBmp, String content, int QR_WIDTH, int QR_HEIGHT) throws WriterException { try { // 圖像數據轉換,使用了矩陣轉換 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容錯率 hints.put(EncodeHintType.MARGIN, 2); // default is 4 hints.put(EncodeHintType.MAX_SIZE, 350); hints.put(EncodeHintType.MIN_SIZE, 100); BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { // 下面這裡按照二維碼的算法,逐個生成二維碼的圖片,//兩個for循環是圖片橫列掃描的結果 for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) pixels[y * QR_WIDTH + x] = 0xff000000; else pixels[y * QR_WIDTH + x] = 0xffffffff; } } // ------------------添加圖片部分------------------// Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); // 設置像素點 bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); // 獲取圖片寬高 int logoWidth = logoBmp.getWidth(); int logoHeight = logoBmp.getHeight(); if (QR_WIDTH == 0 || QR_HEIGHT == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return bitmap; } // 圖片繪制在二維碼中央,合成二維碼圖片 // logo大小為二維碼整體大小的1/2 float scaleFactor = QR_WIDTH * 1.0f / 2 / logoWidth; try { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, 0, null); canvas.scale(scaleFactor, scaleFactor, QR_WIDTH / 2, QR_HEIGHT / 2); canvas.drawBitmap(logoBmp, (QR_WIDTH - logoWidth) / 2, (QR_HEIGHT - logoHeight) /2, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return bitmap; } catch (Exception e) { bitmap = null; e.getStackTrace(); } } catch (WriterException e) { e.printStackTrace(); } return null; }

從上段程式碼可以看出,要給二維碼圖片中央加 logo,但圖片不能佔據整個二維碼圖片很大一部分。然後還必須設置容錯率:容錯率有 M、L、Q、H 幾個等級,容錯率越高,二維碼的有效像素點就越多。這裡使用小寫的 utf-8 編碼,大寫會出現 ]Q2\000026 開頭的內容,為了好看點還設置了邊距和大小。

第三步:實現帶 logo 的彩色二維碼

接下來我們把黑白矩陣變為彩色矩陣:
就把

 if (bitMatrix.get(x, y)) pixels[y * width + x] = 0xff000000; else pixels[y * width + x] = 0xffffffff;

替換為:(這裡的顏色隨便設置,效果隨便改)

 if (x < QR_WIDTH / 2 && y < QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF0094FF;// 藍色 Integer.toHexString(new Random().nextInt()); } else if (x < QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFFFED545;// 黃色 } else if (x > QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF5ACF00;// 綠色 } else { pixels[y * QR_WIDTH + x] = 0xFF000000;// 黑色 } } else { pixels[y * QR_WIDTH + x] = 0xffffffff;// 白色 } 

改後的效果:

第四步:給二維碼加背景

接下來我們來給二維碼圖片加背景:

 /** * 給二維碼圖片加背景 * */ public static Bitmap addBackground(Bitmap foreground,Bitmap background){ int bgWidth = background.getWidth(); int bgHeight = background.getHeight(); int fgWidth = foreground.getWidth(); int fgHeight = foreground.getHeight(); Bitmap newmap = Bitmap .createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newmap); canvas.drawBitmap(background, 0, 0, null); canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2, (bgHeight - fgHeight) *3 / 5+70, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return newmap; }

這樣效果就變為:


第五步:給二維碼加水印

然後二維碼的個性化制作就到最後一步了:加水印,位置隨便放

 /** * 在圖片右下角添加水印 * * @param srcBMP * 原圖 * @param markBMP * 水印圖片 * @return 合成水印後的圖片 */ public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) { if (srcBMP == null) { return null; } // 創建一個新的和SRC長度寬度一樣的位圖 Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(), srcBMP.getHeight(), Bitmap.Config.ARGB_8888); Canvas cv = new Canvas(newb); // 在 0,0坐標開始畫入原圖 cv.drawBitmap(srcBMP, 0, 0, null); // 在原圖的右下角畫入水印 cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth()*4/5, srcBMP.getHeight()*2/7 , null); // 保存 cv.save(Canvas.ALL_SAVE_FLAG); // 存儲 cv.restore(); return newb; }

 

這裡貼下實現二維碼個性化的完整程式碼類:

 package com.ry.personalizedcode.uitls; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import java.util.Hashtable; import java.util.Random; /** * Created on 2016/2/24. * 生成二維碼的工具類 */ public class MakeQRCodeUtil { /** * 根據指定內容生成自定義寬高的二維碼圖片 * * param logoBm * logo圖標 * param content * 需要生成二維碼的內容 * param width * 二維碼寬度 * param height * 二維碼高度 * throws WriterException * 生成二維碼異常 */ public static Bitmap makeQRImage(Bitmap logoBmp, String content, int QR_WIDTH, int QR_HEIGHT) throws WriterException { try { // 圖像數據轉換,使用了矩陣轉換 Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 容錯率 hints.put(EncodeHintType.MARGIN, 2); // default is 4 hints.put(EncodeHintType.MAX_SIZE, 350); hints.put(EncodeHintType.MIN_SIZE, 100); BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, QR_WIDTH, QR_HEIGHT, hints); int[] pixels = new int[QR_WIDTH * QR_HEIGHT]; for (int y = 0; y < QR_HEIGHT; y++) { // 下面這裡按照二維碼的算法,逐個生成二維碼的圖片,//兩個for循環是圖片橫列掃描的結果 for (int x = 0; x < QR_WIDTH; x++) { if (bitMatrix.get(x, y)) { if (x < QR_WIDTH / 2 && y < QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF0094FF;// 藍色 Integer.toHexString(new Random().nextInt()); } else if (x < QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFFFED545;// 黃色 } else if (x > QR_WIDTH / 2 && y > QR_HEIGHT / 2) { pixels[y * QR_WIDTH + x] = 0xFF5ACF00;// 綠色 } else { pixels[y * QR_WIDTH + x] = 0xFF000000;// 黑色 } } else { pixels[y * QR_WIDTH + x] = 0xffffffff;// 白色 } } } // ------------------添加圖片部分------------------// Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT, Bitmap.Config.ARGB_8888); // 設置像素點 bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT); // 獲取圖片寬高 int logoWidth = logoBmp.getWidth(); int logoHeight = logoBmp.getHeight(); if (QR_WIDTH == 0 || QR_HEIGHT == 0) { return null; } if (logoWidth == 0 || logoHeight == 0) { return bitmap; } // 圖片繪制在二維碼中央,合成二維碼圖片 // logo大小為二維碼整體大小的1/2 float scaleFactor = QR_WIDTH * 1.0f / 2 / logoWidth; try { Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, 0, null); canvas.scale(scaleFactor, scaleFactor, QR_WIDTH / 2, QR_HEIGHT / 2); canvas.drawBitmap(logoBmp, (QR_WIDTH - logoWidth) / 2, (QR_HEIGHT - logoHeight) /2, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return bitmap; } catch (Exception e) { bitmap = null; e.getStackTrace(); } } catch (WriterException e) { e.printStackTrace(); } return null; } /** * 獲取十六進制的顏色代碼.例如 "#6E36B4" , For HTML , * @return String */ public static String getRandColorCode(){ String r,g,b; Random random = new Random(); r = Integer.toHexString(random.nextInt(256)).toUpperCase(); g = Integer.toHexString(random.nextInt(256)).toUpperCase(); b = Integer.toHexString(random.nextInt(256)).toUpperCase(); r = r.length()==1 ? "0" + r : r ; g = g.length()==1 ? "0" + g : g ; b = b.length()==1 ? "0" + b : b ; return r+g+b; } /** * 根據指定內容生成自定義寬高的二維碼圖片 * * @param content * 需要生成二維碼的內容 * @param width * 二維碼寬度 * @param height * 二維碼高度 * @throws WriterException * 生成二維碼異常 */ public static Bitmap makeQRImage(String content, int width, int height) throws WriterException { Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // 圖像數據轉換,使用了矩陣轉換 BitMatrix bitMatrix = new QRCodeWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); int[] pixels = new int[width * height]; // 按照二維碼的算法,逐個生成二維碼的圖片,兩個for循環是圖片橫列掃描的結果 for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { if (bitMatrix.get(x, y)) pixels[y * width + x] = 0xff000000; else pixels[y * width + x] = 0xffffffff; } } // 生成二維碼圖片的格式,使用ARGB_8888 Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bitmap.setPixels(pixels, 0, width, 0, 0, width, height); return bitmap; } /** * 從資源文件中獲取圖片 * * @param context * 上下文 * @param drawableId * 資源文件id * @return */ public static Bitmap gainBitmap(Context context, int drawableId) { Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), drawableId); return bmp; } /** * 在圖片右下角添加水印 * * @param srcBMP * 原圖 * @param markBMP * 水印圖片 * @return 合成水印後的圖片 */ public static Bitmap composeWatermark(Bitmap srcBMP, Bitmap markBMP) { if (srcBMP == null) { return null; } // 創建一個新的和SRC長度寬度一樣的位圖 Bitmap newb = Bitmap.createBitmap(srcBMP.getWidth(), srcBMP.getHeight(), Bitmap.Config.ARGB_8888); Canvas cv = new Canvas(newb); // 在 0,0坐標開始畫入原圖 cv.drawBitmap(srcBMP, 0, 0, null); // 在原圖的右下角畫入水印 cv.drawBitmap(markBMP, srcBMP.getWidth() - markBMP.getWidth()*4/5, srcBMP.getHeight()*2/7 , null); // 保存 cv.save(Canvas.ALL_SAVE_FLAG); // 存儲 cv.restore(); return newb; } /** * 給二維碼圖片加背景 * */ public static Bitmap addBackground(Bitmap foreground,Bitmap background){ int bgWidth = background.getWidth(); int bgHeight = background.getHeight(); int fgWidth = foreground.getWidth(); int fgHeight = foreground.getHeight(); Bitmap newmap = Bitmap .createBitmap(bgWidth, bgHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(newmap); canvas.drawBitmap(background, 0, 0, null); canvas.drawBitmap(foreground, (bgWidth - fgWidth) / 2, (bgHeight - fgHeight) *3 / 5+70, null); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return newmap; } }

第六步:給二維碼實現長按功能

最後,為了模擬一下 QQ 的查看二維碼名片功能,還加了一個長按彈出 actionSheet 的功能。
看效果:

 

具體的安卓版 actionSheet 實現,前面博客有介紹,需要的請移步。

這裡我們先來實現發送給好友功能:(這裡就不做第三方的發送了)

 private void sendToFriends() { Intent intent=new Intent(Intent.ACTION_SEND); Uri imageUri= Uri.parse(Environment.getExternalStorageDirectory()+"/code/qrcode.jpg"); intent.setType("image/*"); intent.putExtra(Intent.EXTRA_STREAM, imageUri); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(Intent.createChooser(intent, getTitle())); }

發送給朋友效果圖:

 

然後就是要實現保存到本地圖庫的功能:

 /** * 先保存到本地再廣播到圖庫 * */ public static void saveImageToGallery(Context context, Bitmap bmp) { // 首先保存圖片 File appDir = new File(Environment.getExternalStorageDirectory(), "code"); if (!appDir.exists()) { appDir.mkdir(); } String fileName = "qrcode.jpg"; file = new File(appDir, fileName); try { FileOutputStream fos = new FileOutputStream(file); bmp.compress(CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // 其次把文件插入到系統圖庫 try { MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null); // 最後通知圖庫更新 context.sendBroadcast(new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file))); } catch (FileNotFoundException e) { e.printStackTrace(); } }

總結:

這一篇說白了就是大量運用了 Canvas 與 Bitmap 的處理,篇幅也稍長一些,讀起來多少有點累。要看完整的程式碼請自行下載 PersonalizedCode.rar。下一篇我準備寫 webView 中的二維碼圖片長按識別功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved