王学岗csdn二维码

先看布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:orientation="vertical"
    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.acer.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:orientation="horizontal"
        >
        <Button
            android:id="@+id/bendi"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="保存到本地"/>
        <Button
            android:id="@+id/pengyouquan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="分享到朋友圈"/>

    </LinearLayout>
    <ImageView
        android:id="@+id/iv_image"
        android:src="@mipmap/ic_launcher"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</LinearLayout>

在看代码,注意添加zxing包

package com.example.acer;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import java.io.UnsupportedEncodingException;
import java.util.Hashtable;
import java.util.Map;


/**
 * 1,画出我们的二维码(二维码用来存数据的,使用谷歌提供的zxing包),使用图片颜色个性化我们的二维码
 */

public class MainActivity extends AppCompatActivity {

    private ImageView mIv_image;
    private BitMatrix mMatrix;
    private int blackColor=0xff000000;
    private int whiteColor=0xffffffff;
    private Bitmap mBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIv_image = (ImageView) findViewById(R.id.iv_image);
        //二维码保存的neiong
        String message = "足浴店";
        Bitmap bitmapCode = null;
        try {
            bitmapCode = createCode(new String(message.getBytes(),"ISO-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        mIv_image.setImageBitmap(bitmapCode);
    }

    //创建二维码的方法,使用zxing中的算法和类,
    public Bitmap createCode(String message) {
        //使用矩阵
        try {

            //         ErrorCorrectionLevel可以设置四个类型中的一种类型
            Map<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<>();
            hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            //把信息转成矩阵;BarcodeFormat.QR_CODE表示二维码,300表示长度和宽度
            mMatrix = new MultiFormatWriter().encode(message, BarcodeFormat.QR_CODE, 300, 300, hintMap);
            //获取二维码的高度和宽度
            int width = mMatrix.getWidth();
            int height = mMatrix.getHeight();
            //把矩阵数据保存成一维数据
            int[] pixels = new int[width * height];
            for (int y = 0; y < height; y++) {
                for (int x = 0; x < width; x++) {
                    if(mMatrix.get(x,y)){
                        pixels[y*height+x]=blackColor;
                    }else{
                        pixels[y*height+x]=whiteColor;
                    }
                }
            }
            //把矩阵转换成bitmap图片
            mBitmap = Bitmap.createBitmap(pixels,300,300, Bitmap.Config.ARGB_4444);
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return mBitmap;
    }
}

现在看下运行效果
这里写图片描述
虽然生成了二维码,但还是远远达不到我们的要求,下面我们看看如何生成个性化的二维码。
我们把黑白颜色修改如下

 private int blackColor=0xffCA2C68;
    private int whiteColor=0xff0EAC51;

看看运行结果
这里写图片描述
颜色虽然很丑,但是效果大家看到了吧!
下面我们给二维码设置一个背景

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值