zoukankan      html  css  js  c++  java
  • android二维码生成

    前生:

    一维码:条形码  数字

    缺点:不好看,占面积,

    好了,请看效果图:

    在准备之前我们要导一个包:core-3.2.1.jar 下载请访问: http://download.csdn.net/download/wiseant/9136099

    或者直接百度.当然不止这一个jar包能实现。市场上还是有很多好用的,在这里我们就用这个;

    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"
        tools:context="com.example.erweima.MainActivity" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="生成" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button1"
            android:layout_alignBottom="@+id/button1"
            android:layout_alignParentRight="true"
            android:layout_marginRight="19dp"
            android:ems="10" />
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="435dp"
            android:layout_height="435dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true" />
    
    </RelativeLayout>

    MainActivity.java  //里面的计算方法都是死的,所以照搬就是!!

    package com.example.erweima;
    
    import java.util.Hashtable;
    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 android.app.Activity;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.Matrix;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageView;
    
    
    public class MainActivity extends Activity {
    
        private EditText et1;
        private Button btn1;
        private ImageView iv1;
        
        private static final int IMAGE_HALFWIDTH = 50;//图片宽度值大小
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et1 = (EditText)findViewById(R.id.editText1);//输入框
            btn1 = (Button)findViewById(R.id.button1);//按钮
            iv1 = (ImageView)findViewById(R.id.imageView1);//生成图片的位置
            btn1.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    //取出字符串
                    String toMakePic_string = et1.getText().toString().trim();
                    Bitmap logo= BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);//二维码中间的图片
                    try {
                        Bitmap bm=createCode(toMakePic_string,logo,BarcodeFormat.QR_CODE);
                        iv1.setImageBitmap(bm);
                    } catch (WriterException e) {
                        e.printStackTrace();
                    }
                    
                }
            });
            
        }
        
        public Bitmap createCode(String string,Bitmap mBitmap, BarcodeFormat format)throws WriterException {
            //Matrix,中文里叫矩阵,在图像处理方面,主要是用于平面的缩放、平移、旋转等操作。
            Matrix m = new Matrix();
            float sx = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getWidth();
            float sy = (float) 2 * IMAGE_HALFWIDTH / mBitmap.getHeight();
            m.setScale(sx, sy);//设置缩放信息
            //将logo图片按martix设置的信息缩放
            mBitmap = Bitmap.createBitmap(mBitmap, 0, 0,mBitmap.getWidth(), mBitmap.getHeight(), m, false);
            MultiFormatWriter writer = new MultiFormatWriter();
            Hashtable hst = new Hashtable();
            hst.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置字符编码
            //生成二维码矩阵信息
            BitMatrix matrix = writer.encode(string, format, 800, 800, hst);
            int width = matrix.getWidth();//矩阵高度
            int height = matrix.getHeight();//矩阵宽度
            int halfW = width / 2;
            int halfH = height / 2;
            int[] pixels = new int[width * height];//定义数组长度为矩阵高度*矩阵宽度,用于记录矩阵中像素信息
            for (int y = 0; y < height; y++) {//从行开始迭代矩阵
                for (int x = 0; x < width; x++) {//迭代列
                    if (x > halfW - IMAGE_HALFWIDTH && x < halfW + IMAGE_HALFWIDTH && y > halfH - IMAGE_HALFWIDTH && y < halfH + IMAGE_HALFWIDTH) {
                        //该位置用于存放图片信息
                        //记录图片每个像素信息
                        pixels[y * width + x] = mBitmap.getPixel(x - halfW+ IMAGE_HALFWIDTH, y - halfH + IMAGE_HALFWIDTH); 
                    }
                    else {
                        if (matrix.get(x, y)) {
                            //如果有黑块点,记录信息
                            pixels[y * width + x] = 0xff000000;//记录黑块信息
                        }
                    }
                }
            }
            Bitmap bitmap = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);
            // 通过像素数组生成bitmap
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
            return bitmap;
        }
    
    }
  • 相关阅读:
    Trie树-0/1字典树-DFS-1624. 最大距离
    图-连通分量-DFS-749. 隔离病毒
    贪心-谷歌-857. 雇佣 K 名工人的最低成本
    图-搜索-DFS-51. N皇后
    图-连通分量-DFS-并查集-695. 岛屿的最大面积
    图-最小生成树-629. 最小生成树
    codeforces 493 D Vasya and Chess【 博弈 】
    POJ 2155 Matrix【 二维树状数组 】
    HDU 5273 Dylans loves sequence【 树状数组 】
    POJ 1195 Mobile phones【 二维树状数组 】
  • 原文地址:https://www.cnblogs.com/896240130Master/p/6228159.html
Copyright © 2011-2022 走看看