Android之二维码生成实现

一:先上效果图、

二:实现步骤、

1.xml的实现、

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f0f0f0"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="49dp"
        android:background="#ffffff">

        <ImageView
            android:id="@+id/imgfh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:padding="5dp"
            android:src="@drawable/farm_nav_fanhui" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="优惠券"
            android:textColor="#333333"
            android:textSize="18dp" />

    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#d9dad7" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="9dp"
        android:background="#f0f0f0" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#e6e6e6" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="49dp"
        android:background="#ffffff">

        <ImageView
            android:id="@+id/juan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="12dp"
            android:src="@drawable/haojuan_saoma_icon" />

        <TextView
            android:id="@+id/titlezhe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/juan"
            android:text="七天连锁酒店7.5折优惠券"
            android:textColor="#262626"
            android:textSize="16dp" />
    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#e6e6e6" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="36dp"
        android:background="#ffffff"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="12dp"
            android:text="有效期:"
            android:textColor="#808080"
            android:textSize="14dp" />

        <TextView
            android:id="@+id/yxtime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="2016.12.30-2017.1.16"
            android:textColor="#808080"
            android:textSize="14dp" />
    </LinearLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#e6e6e6" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="228dp"
        android:background="#ffffff">

        <RelativeLayout
            android:id="@+id/relativeLayout1"
            android:layout_width="165dp"
            android:layout_height="165dp"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="22dp"
            android:background="@drawable/sjzsewm">

            <ImageView
                android:id="@+id/img"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/relativeLayout1"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"
            android:text="向商家出示扫码即可消费"
            android:textColor="#343434"
            android:textSize="14dp" />

    </RelativeLayout>

    <View
        android:layout_width="fill_parent"
        android:layout_height="0.5dp"
        android:background="#e6e6e6" />

</LinearLayout>

2.二维码外围转角、

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- 背景颜色 -->
    <solid android:color="#ffffff" />

    <!-- 控制边界线颜色和大小 -->
    <stroke
        android:width="1dp"
        android:color="#e6e6e6" />

    <!-- 控制圆角大小 -->
    <corners android:radius="4dp" />

</shape>

3.activity的实现、
 

package com.zjtd.bzcommunity.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.zjtd.bzcommunity.R;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2017/1/16.
 * 优惠券扫描
 */
public class YhqsmActivity extends Activity implements View.OnClickListener {

    private ImageView imgfh;//返回键
    private TextView titlezhe;//title和打折类型
    private TextView yxtime;//有效期
    private ImageView result = null;//二维码
    private Bitmap bmp = null;//bitmap
    private String content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yhqsm_layout);
        initlayout();
    }

    /**
     * 实例化
     */
    private void initlayout() {
        imgfh = (ImageView) findViewById(R.id.imgfh);
        titlezhe = (TextView) findViewById(R.id.titlezhe);
        yxtime = (TextView) findViewById(R.id.yxtime);
        result = (ImageView) findViewById(R.id.img);
        imgfh.setOnClickListener(this);
        content = "你来打我呀!";
        try {
            bmp = createBitmap(Create2DCode(content));
        } catch (WriterException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        result.setImageBitmap(bmp);
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();

        bmp.compress(Bitmap.CompressFormat.PNG, 100, baos1);

        try {
            Writetemp(baos1.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回键
            case R.id.imgfh:
                finish();
                break;
        }
    }

    public Bitmap Create2DCode(String str) throws WriterException,
            UnsupportedEncodingException {

        BitMatrix matrix = new MultiFormatWriter().encode(
                new String(str.getBytes("GBK"), "ISO-8859-1"),
                BarcodeFormat.QR_CODE, 300, 300);

        int width = matrix.getWidth();
        int height = matrix.getHeight();
        int[] pixels = new int[width * height];
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                if (matrix.get(x, y)) {
                    pixels[y * width + x] = 0xff000000;
                }

            }
        }
        int[] colors = {R.color.white};
        Bitmap bitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return bitmap;
    }

    public void Write(byte[] b) throws IOException {
        File cacheFile = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File sdCardDir = Environment.getExternalStorageDirectory();

            long time = Calendar.getInstance().getTimeInMillis();
            String fileName = time + ".png";
            File dir = new File(sdCardDir.getCanonicalPath() + "/zibuyu/");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            cacheFile = new File(dir, fileName);

        }
        Toast toast = Toast.makeText(getApplicationContext(), "哈哈哈哈",
                Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER, 0, 0);
        LinearLayout toastView = (LinearLayout) toast.getView();
        ImageView imageCodeProject = new ImageView(getApplicationContext());
        toastView.addView(imageCodeProject, 0);
        toast.show();
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
            bos.write(b, 0, b.length);
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void Writetemp(byte[] b) throws IOException {
        File cacheFile = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            File sdCardDir = Environment.getExternalStorageDirectory();
            Date curDate = new Date(System.currentTimeMillis());
            String fileName = "temp.jpg";
            File dir = new File(sdCardDir.getCanonicalPath() + "/zibuyu/temp");
            if (!dir.exists()) {
                dir.mkdirs();
            }
            cacheFile = new File(dir, fileName);

        }
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(cacheFile));

            bos.write(b, 0, b.length);
            bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Bitmap createBitmap(Bitmap src) {
        if (src == null) {
            return null;
        }
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setAntiAlias(true);

        int w = 300;
        int h = 300;
        Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas cv = new Canvas(newb);

        cv.drawColor(Color.WHITE);

        cv.drawBitmap(src, 0, 0, null);
        cv.save(Canvas.ALL_SAVE_FLAG);
        cv.restore();
        return newb;

    }
}

 

 

 

就只需简单几步就OK了!不会的可以学习一下,大神就别喷了、、、、

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶已初秋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值