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;
} }
android二维码生成的更多相关文章
- android 二维码生成+扫描
android 二维码生成+扫描 1.在Android应用当中,很多时候都要用到二维码扫描,来避免让用户手动输入的麻烦. Google官方自己推出了一个二维码开源项目:ZXing库. 2.这里简单介绍 ...
- Android 二维码 生成和识别(附Demo源码)
今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. Android.WP都有相关支持的软件.之前我就想了解二维码是如何工作,最近因为工作需要使用相关技 ...
- Android 二维码 生成和识别(转)
原博客地址 :http://www.cnblogs.com/weixing/archive/2013/08/28/3287120.html 还有几个写的也可以参考一下:http://www.itnos ...
- 【转】Android 二维码 生成和识别(附Demo源码)--不错
原文网址:http://www.cnblogs.com/mythou/p/3280023.html 今天讲一下目前移动领域很常用的技术——二维码.现在大街小巷.各大网站都有二维码的踪迹,不管是IOS. ...
- android 二维码生成
1,activity public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set ...
- 玩转Android之二维码生成与识别
二维码,我们也称作QRCode,QR表示quick response即快速响应,在很多App中我们都能见到二维码的身影,最常见的莫过于微信了.那么今天我们就来看看怎么样在我们自己的App中集成二维码的 ...
- Android二维码开源项目zxing用例简化和生成二维码、条形码
上一篇讲到:Android二维码开源项目zxing编译,编译出来后有一个自带的測试程序:CaptureActivity比較复杂,我仅仅要是把一些不用的东西去掉,用看起来更方便,二维码和条形码的流行性自 ...
- Android二维码扫描、生成
Android二维码扫描.生成 现在使用二维码作为信息的载体已经越来越普及,那么二维码的生成以及扫描是如何实现的呢 google为我们提供了zxing开源库供我们使用 zxing GitHub源码地址 ...
- Android开发——Android中的二维码生成与扫描
0. 前言 今天这篇文章主要描述二维码的生成与扫描,使用目前流行的Zxing,为什么要讲二维码,因为二维码太普遍了,随便一个Android APP都会有二维码扫描.本篇旨在帮助有需求的同学快速完成二维 ...
随机推荐
- 【postman】postman
谷歌插件网 http://chromecj.com/utilities/2014-09/181.html
- AD域-让共享目录只显示用户有权限访问的文件夹
问题: 在AD域中,我们一般都会用到共享,如果有很多部门,我们可能还会按部门.职位配置权限.比如CSD,IT,PA等,但文件夹一多,用户看着就头大,而且用户没权限访问的文件夹误点击进去还会提示无权限访 ...
- R语言作为BI中ETL的工具
R语言作为BI中ETL的工具,增删改 R语言提供了强大的R_package与各种数据库进行数据交互. 外加其强大数据变换清洗函数,为ETL提供一条方便快捷的道路. RODBC ROracal RMys ...
- Json在PHP与JS之间传输
1. JS-->PHP a). JS create Json <script> $(document).ready(function(){ /*--JS create Json--* ...
- nmq消息队列解析
消息中间件NMQ 1.What is nmq? nmq = new message queue; 一个通用消息队列系统 为在线服务设计 什么是消息队列?问什么需要?有哪些功能? 消息队列的本质:1.多 ...
- VB常用字符串操作函数
1. ASC(X),Chr(X):转换字符字符码 [格式]: P=Asc(X) 返回字符串X的第一个字符的字符码 P=Chr(X) 返回字符码等于X的字符 [范例]: (1)P=Chr(65) ‘ 输 ...
- Lisp永远成不了编程主流语言
Lisp语言是第二古老的高级编程语言.许多的黑客和开发者对Lisp推崇备至,Paul Graham甚至说"编程语言现在的发展,不过刚刚赶上1958年Lisp语言的水平". ...
- BZOJ 2081: [Poi2010]Beads
Description 问把n截成每个长度后不同子串个数. Sol 调和极数+Hash. 首先这是一个式子 \(n\sum_{i=1}^n \frac {1}{i}\) . 这东西就是调和极数再乘上 ...
- Android app AOP添加埋点技术总结
目标:通过面向切面编程来实现对源代码无侵入的埋点. 方式 能力 缺点 学习曲线 XPosed 运行期hook 能hook自己应用进程的方法: 能hook别的应用的方法: 能hook系统方法 ...
- C++指针参数引用
粘个代码占位置,以后有时间把指针函数,函数指针都补上 #include <iostream> using namespace std; void freePtr1(int* p1){ /* ...