java-二维码编写zxing
zxing 这个是google的
下载地址
http://code.google.com/p/zxing/downloads/list
二维码源码案例
package com.utils; import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix; public class UtilsQrCode { private static final int BLACK = 0xff000000;
private static final int WHITE = 0xFFFFFFFF; /**
* @param args
*/
public static void main(String[] args) {
File file = new File("C:\\Users\\huage\\Desktop\\图片\\"+new Date().getTime()+".png");
encode("http://www.baidu.com", file, BarcodeFormat.QR_CODE, 200, 200);
} /**
* @param contents
* @param file
* @param format
* @param width
* @param height
*/
public static void encode(String contents, File file, BarcodeFormat format, int width, int height) {
try {
contents = new String(contents.getBytes("UTF-8"), "ISO-8859-1");
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, format, width, height,hints);
BufferedImage image = toBufferedImage(bitMatrix);
writeToFile(image, "png", file);
} catch (Exception e) {
e.printStackTrace();
}
} public static byte[] imgToBytes(BufferedImage image){
if( image == null ) return new byte[0];
byte[] data = ((DataBufferByte) image.getData().getDataBuffer()).getData();
return data;
} public static void writeToFile(BufferedImage image, String format, File file) throws IOException {
if( image == null || file == null ) return;
ImageIO.write(image, format, file);
} public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) == true ? BLACK : WHITE);
}
}
return image;
} }
java-二维码编写zxing的更多相关文章
- java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍 我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...
- Atitit java 二维码识别 图片识别
Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- Java二维码生成与解码工具Zxing使用
Zxing是Google研发的一款非常好用的开放源代码的二维码生成工具,目前源码托管在github上,源码地址: https://github.com/zxing/zxing 可以看到Zxing库有很 ...
- Java生成带小图标的二维码-google zxing 工具类
近期一直忙于开发微信商城项目,应客户要求,要开发个有图标的二维码.经过两次改版,终于实现了该功能(第一次没有小图标,这次才整合好的),如下是完整代码 . 该代码使用Java7开发,另外使用 core- ...
- java二维码之利用谷歌的zxing生成二维码,解析二维码
生成二维码 @RequestMapping("/123") public void test(HttpServletRequest request,HttpServletRespo ...
- java二维码小试牛刀
旁白: 由于工作需要,要做一个java的二维码的图片,花了2天的时间学习了一下,过程中也遇到了一些问题,这里做个笔记,收藏了. 废话不多说了,入题吧! 转自:http://www.open-open. ...
- Java二维码登录流程实现(包含短地址生成,含部分代码)
近年来,二维码的使用越来越风生水起,笔者最近手头也遇到了一个需要使用二维码扫码登录网站的活,所以研究了一下这一套机制,并用代码实现了整个流程,接下来就和大家聊聊二维码登录及的那些事儿. 二维码原理 二 ...
- java二维码开发
之前就写过很多关于二维码的东西,一直没有时间整理一下,所以呢今天就先来介绍一下如何利用java开发二维码.生成二维码有很多jar包可以实现,例如Zxing,QRcode,前者是谷歌的,后者日本的,这里 ...
随机推荐
- JAVA中的单利
单列:单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种.单例模式有一下特点:1.单例类只能有一个实例.2.单例类必须自己自己创建自己的唯一实例.3.单例类必须给所有其他对象提供这一实例. 一.懒 ...
- Eclipse如何解决启动慢
一般在不对eclipse进行相关设置的时候,使用eclipse总是会觉得启动好慢,用起来好卡,其实只要对eclipse的相关参数进行一些配置,就会有很大的改善. 加快启动速度 1.在eclipse启 ...
- windows下使用 linux命令好办法
1. 安装下载 CygwinPortable一键安装包.7z 2. 把安装路径下/ [D:\cygwinportable\CygwinPortable\App\Cygwin\bin] 加到 Path ...
- Android应用程序(APK)的编译打包过程
(9878) (7) 现在很多人想对Android工程的编译和打包进行自动化,比如建立每日构建系统.自动生成发布文件等等.这些都需要我们对Android工程的编译和打包有一个深入的理解,至少要知道它的 ...
- 使用gulp将移动端px转为rem
使用gulp的插件可以很方便的将xp转为rem,在布局的时候使用@1x .@2x布局,即10rem=device-width:@1x即设计图为320px,1rem对应的10px像素,相对的@2x即为布 ...
- Kubernetes deployed on multiple ubuntu nodes
This document describes how to deploy kubernetes on multiple ubuntu nodes, including 1 master node a ...
- PAT 1007. 素数对猜想 (20)
让我们定义 dn 为:dn = pn+1 - pn,其中 pi 是第i个素数.显然有 d1=1 且对于n>1有 dn 是偶数."素数对猜想"认为"存在无穷多对相邻且 ...
- PAT 1003. 我要通过!(20) JAVA
参考http://blog.csdn.net/bin8632/article/details/50216297 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答 ...
- Inline Workers--Web workers without a separate Javascript file
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...
- caffe的python接口学习(3):训练模型(training)
如果不进行可视化,只想得到一个最终的训练model, 那么代码非常简单,如下 : import caffe caffe.set_device(0) caffe.set_mode_gpu() solve ...