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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-24 我要评论
想了解Android 带logo的二维码详解及实例的相关内容吗,在本文为您仔细讲解Android 带logo的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,带logo的二维码详解,Android,logo二维码,下面大家一起来学习吧。

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

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们