亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android 帶logo的二維碼詳解及實(shí)例

 更新時(shí)間:2017年05月23日 11:32:40   投稿:lqh  
這篇文章主要介紹了Android 帶logo的二維碼詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下

Android 帶logo的二維碼詳解及實(shí)例

好久沒(méi)有寫(xiě)博客了,快元旦了公司的事情也不是很多,剛好和朋友一起出去玩玩,朋友是搞PHP的說(shuō)到了每天在公司都是搞些什么二維碼和微信支付的相關(guān)東西,因?yàn)樯习嗟臅r(shí)間不忙,所以隨便來(lái)搞下。

二維碼(Quick Response Code),又稱二維條碼,它是用特定的幾何圖形按一定規(guī)律在平面(二維方向)上分布的黑白相間的圖形,是所有信息數(shù)據(jù)的一把鑰匙。在現(xiàn)代商業(yè)活動(dòng)中,如果一個(gè)產(chǎn)品是不能通過(guò)二維碼來(lái)進(jìn)行訪問(wèn)什么的,顯然是不成功的。用的比較多的生成二維碼的jar包有Zxing.jar和core.jar,其實(shí)里面用到的都是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();
      // 把輸入的文本轉(zhuǎn)為二維碼
      BitMatrix martix = writer.encode(text, BarcodeFormat.QR_CODE,
          QR_WIDTH, QR_HEIGHT);
     //圖像數(shù)據(jù)轉(zhuǎn)換,使用了矩陣轉(zhuǎn)換
    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++) {
//下面這里按照二維碼的算法,逐個(gè)生成二維碼的圖片,//兩個(gè)for循環(huán)是圖片橫列掃描的結(jié)果
        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);
//設(shè)置像素點(diǎn)
      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();
    }
  }
 
}

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

相關(guān)文章

最新評(píng)論