国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 帶logo的二維碼詳解及實例

2019-10-23 18:28:09
字體:
來源:轉載
供稿:網友

Android 帶logo的二維碼詳解及實例

好久沒有寫博客了,快元旦了公司的事情也不是很多,剛好和朋友一起出去玩玩,朋友是搞PHP的說到了每天在公司都是搞些什么二維碼和微信支付的相關東西,因為上班的時間不忙,所以隨便來搞下。

二維碼(Quick Response Code),又稱二維條碼,它是用特定的幾何圖形按一定規律在平面(二維方向)上分布的黑白相間的圖形,是所有信息數據的一把鑰匙。在現代商業活動中,如果一個產品是不能通過二維碼來進行訪問什么的,顯然是不成功的。用的比較多的生成二維碼的jar包有Zxing.jar和core.jar,其實里面用到的都是com.google.zxing里面的東西,基本上是大同小異。

直接上代碼:

activity_main.xml

<RelativeLayout xmlns:Android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="com.example.MainActivity" >  <ImageView    android:id="@+id/code"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:text="@string/hello_world" /></RelativeLayout>

MainActivity

package com.example;import Java.util.Hashtable;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.os.Bundle;import android.widget.ImageView;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;public class MainActivity extends Activity {  private ImageView code;  private final int QR_WIDTH=300;  private final int QR_HEIGHT=300; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); code=(ImageView) findViewById(R.id.code); createImage("weixin") ; } // 生成QR圖  private void createImage(String text) {    try {      // 需要引入core包      QRCodeWriter writer = new QRCodeWriter();      // 把輸入的文本轉為二維碼      BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,          QR_WIDTH, QR_HEIGHT);     //圖像數據轉換,使用了矩陣轉換    Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();      hints.put(EncodeHintType.CHARACTER_SET, "utf-8");      BitMatrix bitMatrix = new QRCodeWriter().encode(text,          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 logoBmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);      Bitmap bitmap = Bitmap.createBitmap(QR_WIDTH, QR_HEIGHT,          Bitmap.Config.ARGB_8888);//設置像素點      bitmap.setPixels(pixels, 0, QR_WIDTH, 0, 0, QR_WIDTH, QR_HEIGHT);            Canvas canvas = new Canvas(bitmap);     //二維碼     canvas.drawBitmap(bitmap, 0,0, null);     //圖片繪制在二維碼中央,合成二維碼圖片  canvas.drawBitmap(logoBmp, bitmap.getWidth() / 2   - logoBmp.getWidth() / 2, bitmap.getHeight()   / 2 - logoBmp.getHeight() / 2, null);  //------------------添加logo部分------------------//      code.setImageBitmap(bitmap);         } catch (WriterException e) {      e.printStackTrace();    }  } }

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 玉环县| 怀远县| 从江县| 洛南县| 福建省| 昔阳县| 绥化市| 丰顺县| 会东县| 万荣县| 吉林省| 平舆县| 尼玛县| 芜湖市| 宜宾市| 阳泉市| 凉城县| 巴中市| 甘洛县| 通海县| 化德县| 如皋市| 连云港市| 博湖县| 虞城县| 烟台市| 隆昌县| 手游| 比如县| 滕州市| 镇康县| 韶山市| 西乡县| 乐东| 平利县| 安岳县| 和田县| 谢通门县| 南部县| 兴和县| 吉安市|