温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 带logo的二维码详解及实例

发布时间:2020-08-23 09:20:07 来源:脚本之家 阅读:88 作者:lqh 栏目:移动开发

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();
    }
  }
 
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI