设为首页 加入收藏

TOP

安卓生成二维码 ==。以及中文乱码问题(一)
2017-10-12 17:57:42 】 浏览:9624
Tags:安卓 生成 二维 以及 中文 乱码 问题

参考了http://blog.csdn.net/books1958/article/details/46346531

和http://www.cnblogs.com/mythou/p/3280023.html

 

很多文章用的都是旧的,过时了,找了半天只有这两个是好用的 ==。

zing包是3.1, 链接如下  http://pan.baidu.com/s/1pKfkQVp

 

MainActivity代码如下

package com.example.wang.bmap;

import com.google.zxing.*;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;


public class MainActivity extends AppCompatActivity {




    private Button genButton;
    private ImageView img;
    private EditText input;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        genButton = (Button)findViewById(R.id.gen);
        img = (ImageView)findViewById(R.id.img);
        input = (EditText)findViewById(R.id.input);
        createQR();
    }
    public void createQR(){
        genButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View arg0) {
                        String in = input.getText().toString();
                        try {
                            BitMatrix qrcode = new QRCodeWriter().encode(in, BarcodeFormat.QR_CODE, 400, 400);
                            int[] pixels = new int[400 * 400];

                            for (int y = 0; y < 400; y++) {
                                for (int x = 0; x < 400; x++) {
                                    if (qrcode.get(x, y)) {
                                        pixels[y * 400 + x] = 0xff000000;
                                    } else {
                                        pixels[y * 400 + x] = 0xffffffff;

                                    }
                                }
                            }
                            Bitmap bitmap = Bitmap.createBitmap(400, 400, Bitmap.Config.ARGB_8888);
                            bitmap.setPixels(pixels, 0, 400, 0, 0, 400, 400);
                            img.setImageBitmap(bitmap);

                        } catch (WriterException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
    }

/*

*/



}

  

xml代码

<?xml version="1.0" encoding="utf-8"?>
<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.wang.bmap.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="从下边生成二维码" />

    <EditText
        android:id="@+id/input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"
        android:hint="请输入要生成的二维码文字" />

    <Button
        android:id="@+id/gen"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/inp
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇27个漂亮的移动端注册/登录界面设.. 下一篇android 之 启动画面的两种方法

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目