首页 技术 正文
技术 2022年11月11日
0 收藏 730 点赞 4,573 浏览 3787 个字

前生:

一维码:条形码  数字

android二维码生成

缺点:不好看,占面积,

android二维码生成

好了,请看效果图:

android二维码生成

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;
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289