java二维码识别
『壹』 qrcode生成的二维码怎么用java读取
看看这里的讲解。有现成的用qrcode生成二维码和解析二维码:
http://www.sojson.com/tag_erweima.html
二维码线上Demo:
http://www.sojson.com/qr.html
『贰』 如何使摄像头扫描二维码,然后解析二维码 java源码 zxing
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, stream)) {
throw new IOException("Could not write an image of format " + format);
}
}
}
『叁』 Java设置二维码只能用摄像头扫,不能拍照后再扫描
这怎么能识别出来是拍的照片,其实生成的二维码本身就是图片呀,这和根据手机壳颜色换主题有异曲同工之妙哦!
不过办法还是有的,就是需要生成端和扫码端做修改了,二维码动态生成(定时刷新),生成的二维码设置过期时间(也就是二维码信息中带时间戳),这样不同时间生成的二维码是不一样的,扫描后再进行校验是否过期就可以了。
『肆』 谁有java二维码扫描软件
我没太懂你的意思,你是要java写的扫描软件的话,网上有很多啊,微信也可以扫描啊,我这里有一个用java写的生成二维码和解析二维码的工程,研究二维码代码的话可以去google的开源项目zxing看一下,希望对你有帮助。
『伍』 如何识别图片是否是二维码java中js或jq中
js 有专门用来识别QR 码的库 webQrDecodeClass.js 可以引用
『陆』 请问java如何实现二维码一码多识
会场的二维码应该要包含会场id
然后会员扫描二维码,获得会场的id,此时会员必须登录,登录会员就能获得会员id,
然后根据会员id和会场id去跟服务器查询作为信息
『柒』 求一个用Java编的二维码生成识别的demo,希望内容完整点,万分感谢
在java工程里面导入QRCode.JAR包
importjava.awt.Color;
importjava.awt.Graphics2D;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjavax.imageio.ImageIO;
importjp.sourceforge.qrcode.QRCodeDecoder;
importjp.sourceforge.qrcode.exception.DecodingFailedException;
importcom.swetake.util.Qrcode;
publicclassTwoDimensionCode{
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramimgPath
*图片路径
*/
publicvoidencoderQRCode(Stringcontent,StringimgPath){
this.encoderQRCode(content,imgPath,"png",7);
}
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramoutput
*输出流
*/
publicvoidencoderQRCode(Stringcontent,OutputStreamoutput){
this.encoderQRCode(content,output,"png",7);
}
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramimgPath
*图片路径
*@paramimgType
*图片类型
*/
publicvoidencoderQRCode(Stringcontent,StringimgPath,StringimgType){
this.encoderQRCode(content,imgPath,imgType,7);
}
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramoutput
*输出流
*@paramimgType
*图片类型
*/
publicvoidencoderQRCode(Stringcontent,OutputStreamoutput,
StringimgType){
this.encoderQRCode(content,output,imgType,7);
}
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramimgPath
*图片路径
*@paramimgType
*图片类型
*@paramsize
*二维码尺寸
*/
publicvoidencoderQRCode(Stringcontent,StringimgPath,StringimgType,
intsize){
try{
BufferedImagebufImg=this.qRCodeCommon(content,imgType,size);
FileimgFile=newFile(imgPath);
//生成二维码QRCode图片
ImageIO.write(bufImg,imgType,imgFile);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*生成二维码(QRCode)图片
*
*@paramcontent
*存储内容
*@paramoutput
*输出流
*@paramimgType
*图片类型
*@paramsize
*二维码尺寸
*/
publicvoidencoderQRCode(Stringcontent,OutputStreamoutput,
StringimgType,intsize){
try{
BufferedImagebufImg=this.qRCodeCommon(content,imgType,size);
//生成二维码QRCode图片
ImageIO.write(bufImg,imgType,output);
}catch(Exceptione){
e.printStackTrace();
}
}
/**
*生成二维码(QRCode)图片的公共方法
*
*@paramcontent
*存储内容
*@paramimgType
*图片类型
*@paramsize
*二维码尺寸
*@return
*/
(Stringcontent,StringimgType,intsize){
BufferedImagebufImg=null;
try{
QrcodeqrcodeHandler=newQrcode();
//设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
qrcodeHandler.setQrcodeEncodeMode('B');
//设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大
qrcodeHandler.setQrcodeVersion(size);
//获得内容的字节数组,设置编码格式
byte[]contentBytes=content.getBytes("utf-8");
//图片尺寸
intimgSize=67+12*(size-1);
bufImg=newBufferedImage(imgSize,imgSize,
BufferedImage.TYPE_INT_RGB);
Graphics2Dgs=bufImg.createGraphics();
//设置背景颜色
gs.setBackground(Color.WHITE);
gs.clearRect(0,0,imgSize,imgSize);
//设定图像颜色>BLACK
gs.setColor(Color.BLACK);
//设置偏移量,不设置可能导致解析出错
intpixoff=2;
//输出内容>二维码
if(contentBytes.length>0&&contentBytes.length<800){
boolean[][]codeOut=qrcodeHandler.calQrcode(contentBytes);
for(inti=0;i<codeOut.length;i++){
for(intj=0;j<codeOut.length;j++){
if(codeOut[j][i]){
gs.fillRect(j*3+pixoff,i*3+pixoff,3,3);
}
}
}
}else{
thrownewException("QRCodecontentbyteslength="
+contentBytes.length+"notin[0,800].");
}
gs.dispose();
bufImg.flush();
}catch(Exceptione){
e.printStackTrace();
}
returnbufImg;
}
/**
*解析二维码(QRCode)
*@paramimgPath图片路径
*@return
*/
publicStringdecoderQRCode(StringimgPath){
//QRCode二维码图片的文件
FileimageFile=newFile(imgPath);
BufferedImagebufImg=null;
Stringcontent=null;
try{
bufImg=ImageIO.read(imageFile);
QRCodeDecoderdecoder=newQRCodeDecoder();
content=newString(decoder.decode(newTwoDimensionCodeImage(bufImg)),"utf-8");
}catch(IOExceptione){
System.out.println("Error:"+e.getMessage());
e.printStackTrace();
}catch(DecodingFailedExceptiondfe){
System.out.println("Error:"+dfe.getMessage());
dfe.printStackTrace();
}
returncontent;
}
/**
*解析二维码(QRCode)
*@paraminput输入流
*@return
*/
publicStringdecoderQRCode(InputStreaminput){
BufferedImagebufImg=null;
Stringcontent=null;
try{
bufImg=ImageIO.read(input);
QRCodeDecoderdecoder=newQRCodeDecoder();
content=newString(decoder.decode(newTwoDimensionCodeImage(bufImg)),"utf-8");
}catch(IOExceptione){
System.out.println("Error:"+e.getMessage());
e.printStackTrace();
}catch(DecodingFailedExceptiondfe){
System.out.println("Error:"+dfe.getMessage());
dfe.printStackTrace();
}
returncontent;
}
publicstaticvoidmain(String[]args){
StringimgPath="G:/TDDOWNLOAD/Michael_QRCode.png";
StringencoderContent="Hello大大、小小,welcometoQRCode!"+" Myblog[http://sjsky.iteye.com]"+" EMail[[email protected]]";
TwoDimensionCodehandler=newTwoDimensionCode();
handler.encoderQRCode(encoderContent,imgPath,"png");
//try{
//OutputStreamoutput=newFileOutputStream(imgPath);
//handler.encoderQRCode(content,output);
//}catch(Exceptione){
//e.printStackTrace();
//}
System.out.println("========encodersuccess");
StringdecoderContent=handler.decoderQRCode(imgPath);
System.out.println("解析结果如下:");
System.out.println(decoderContent);
System.out.println("========decodersuccess!!!");
}
}
第二个代码:
importjava.awt.image.BufferedImage;
importjp.sourceforge.qrcode.data.QRCodeImage;
{
BufferedImagebufImg;
publicTwoDimensionCodeImage(BufferedImagebufImg){
this.bufImg=bufImg;
}
@Override
publicintgetHeight(){
returnbufImg.getHeight();
}
@Override
publicintgetPixel(intx,inty){
returnbufImg.getRGB(x,y);
}
@Override
publicintgetWidth(){
returnbufImg.getWidth();
}
}
『捌』 如何使用java实现二维码扫描登录微信网页版(微信端)
jsp+spring+struts2+mybatis:
模仿微信网页版扫码登录
使用js代码生成qrcode二维码减轻服务器压力
js循环请求服务端,判断是否qrcode被扫
二维码超时失效功能
二维码被扫成功登录,服务端产生sessionId,传到页面使用js保存cookie
多线程
- ### 生成qrcode相关js jquery.qrcode.js - ### 代码 “
- function keepPool(){
- var uuid = $("#uuid").val();
- $.get(ctx+"/web/login/pool.do",{uuid:uuid,},function(msg){//如果放入一个不存在的网址怎么办?
- //console.log(msg);
- if(msg.successFlag == '1'){
- $("#result").html("扫码成功");
- setCookie(msg.data.cname, msg.data.cvalue, 3*60*60*1000);
- //alert("将跳转...");
- window.location.href = ctx +"/webstage/login/success.do";
- }else if(msg.successFlag == '0'){
- $("#result").html("该二维码已经失效,请重新获取");
- }else{
- keepPool();
- }
- });
- }
- //设置cookie
- function setCookie(cname, cvalue, expireTime) {
- var d = new Date();
- d.setTime(d.getTime() + expireTime);//设置过期时间
- var expires = "expires="+d.toUTCString();
- var path = "path=/"
- document.cookie = cname + "=" + cvalue + "; " + expires + "; " + path;
- }
- //二维码首页public String index() { try {
- uuid = UUID.randomUUID().toString(); super.getRequest().setAttribute("uuid", uuid);
- ScanPool pool = new ScanPool();
- pool.setCreateTime(System.currentTimeMillis());
- Map<String, ScanPool> map = new HashMap<String, ScanPool>(1);
- map.put(uuid, pool);
- PoolCache.cacheMap.put(uuid, pool);
- pool = null;
- } catch (Exception e) {
- Log4jUtil.CommonLog.error("pc生成二维码登录", e);
- } return "index";
- }//判断二维码是否被扫描public void pool() {
- DataResultInfo result = null;
- System.out.println("检测[ " + uuid + " ]是否登录");
- ScanPool pool = null;
- if(MapUtils.isNotEmpty(PoolCache.cacheMap)) pool = PoolCache.cacheMap.get(uuid); try { if (pool == null) { // 扫码超时,进线程休眠
- result = DataResultInfo.getInstance().failure();
- result.setSuccessFlag(CommonConstant.Zero);
- result.setExtension(CommonConstant.Zero, "该二维码已经失效,请重新获取");
- Thread.sleep(10 * 1000L);
- } else { // 使用计时器,固定时间后不再等待扫描结果--防止页面访问超时
- new Thread(new ScanCounter(uuid, pool)).start(); boolean scanFlag = pool.getScanStatus(); //这里得到的ScanPool(时间靠前)和用户使用手机扫码后得到的不是一个,用户扫码后又重新更新了ScanPool对象,并重新放入了redis中,,所以这里要等待上面的计时器走完,才能获得最新的ScanPool
- if (scanFlag) {
- result = DataResultInfo.getSuccess(); // 根据uuid从redis中获取pool对象,得到对应的sessionId,返给页面,通过js存cookie中
- JSONObject jsonObj = new JSONObject();
- jsonObj.put("cname", CookieConstant.SESSION_KEY);
- jsonObj.put("cvalue", pool.getSession());
- result.setData(jsonObj);
- } else {
- result = DataResultInfo.getInstance().failure();
- result.setMessage("等待扫描");
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- sendJsonMessage(result);
- }//手机扫码接口(以id和token作为用户身份登录)
- public String phoneScanLogin() {
- DataResultInfo result = null;
- ScanPool pool = null;
- if(MapUtils.isNotEmpty(PoolCache.cacheMap)) pool = PoolCache.cacheMap.get(uuid); try { if (pool == null) {
- result = DataResultInfo.getInstance().failure();
- result.setMessage("该二维码已经失效,请重新获取");
- } else { if (StringUtils.isNotEmpty(id) && StringUtils.isNotEmpty(token)) { //根据id和token查询后台,获取用户信息userBean
- String redisToken = redisUtil.getRedis(RedisKeyConstant.APP_TOKEN+userId); if(redisToken != null && redisToken.equals(token)){
- UserBean userBean = userService.findByUserId(Long.valueOf(userId)); if (userBean != null) {
- String sessionId = SessionConstant.SESSION_ID_PRE
- + FormatUtils.password(userBean.getId()
- .toString());
- Map<String, String> cookieSession = new HashMap<String, String>();
- cookieSession
- .put(CookieConstant.SESSION_KEY, sessionId); // WrCookie.writeCookie(getResponse(),cookieSession);
- // 添加用户信息到redis
- boolean re = redisUtil.addUserInfo( RedisKeyConstant.SESSION + sessionId, BeanUtils.toBean(userBean, UserInfo.class));
- getSession().setAttribute( SessionConstant.USER_INFO_WEB, BeanUtils.toBean(userBean, UserInfo.class));
- getSession().setAttribute( DomainConstant.USER_CENTER_KEY, DomainConstant.USER_CENTER);
- pool.setSession(sessionId);
- pool.scanSuccess();
- }else{
- result = DataResultInfo.getInstance().failure();
- result.setMessage("用户信息获取异常!请稍后再试");
- }
- } else {
- result = DataResultInfo.getInstance().failure();
- result.setExtension("11", "用户身份信息失效,请重新登录!");
- }
- } else {
- result = DataResultInfo.getInstance().failure();
- result.setMessage("请求参数有误!"); return "error";
- } // 不能清除,否则conn方法得不到pool对象,不会进入线程休眠
- // System.out.println("清除扫描过的uuid");
- //PoolCache.cacheMap.remove(uuid);
- }
- } catch (Exception e) {
- Log4jUtil.CommonLog.error("手机扫码 后访问 异常", e);
- }
- sendJsonMessage(result); return null;
- }//扫码成功跳转页
- public String success() {
- String sessionId = WrCookie.getCookie(super.getRequest(), CookieConstant.SESSION_KEY);
- UserInfo userInfo = redisUtil.getUserInfo(RedisKeyConstant.SESSION + sessionId); super.getRequest().setAttribute(SessionConstant.USER_INFO_WEB, userInfo); return SUCCESS;
- }//线程判断二维码是否超时class ScanCounter implements Runnable { public Long timeout = 30 * 1000L; //超时时长
- // 传入的对象
- private String uuid; private ScanPool scanPool; public ScanCounter(String p, ScanPool scanPool) {
- uuid = p; this.scanPool = scanPool;
- } @Override
- public void run() { try {
- Thread.sleep(timeout);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- notifyPool(uuid, scanPool);
- } public synchronized void notifyPool(String uuid, ScanPool scanPool) { if (scanPool != null) scanPool.notifyPool();
- } public String getUuid() { return uuid;
- } public void setUuid(String uuid) { this.uuid = uuid;
- } public ScanPool getScanPool() { return scanPool;
- } public void setScanPool(ScanPool scanPool) { this.scanPool = scanPool;
- }
- }
- public class ScanPool implements Serializable{
- /**
- * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
- */
- private static final long serialVersionUID = -9117921544228636689L; private Object session ; //创建时间
- private Long createTime = System.currentTimeMillis();
- //登录状态
- private boolean scanFlag = false;
- public boolean isScan(){
- return scanFlag;
- }
- public void setScan(boolean scanFlag){
- this.scanFlag = scanFlag;
- }
- /**
- * 获取扫描状态,如果还没有扫描,则等待固定秒数
- * @param wiatSecond 需要等待的秒数
- * @return
- */
- public synchronized boolean getScanStatus(){
- try
- {
- if(!isScan()){ //如果还未扫描,则等待
- this.wait();
- }
- if (isScan())
- { System.err.println("手机扫描完成设置getScanStatus..true..........."); return true;
- }
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- return false;
- }
- /**
- * 扫码之后设置扫码状态
- * @param token
- * @param id
- */
- public synchronized void scanSuccess(){
- try
- { System.err.println("手机扫描完成setScan(true)....同时释放notifyAll(手机扫码时,根据uuid获得的scanpool对象)");
- setScan(true);
- this.notifyAll();
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public synchronized void notifyPool(){
- try
- {
- this.notifyAll();
- } catch (Exception e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- /***********************************************/
- public Long getCreateTime()
- {
- return createTime;
- }
- public void setCreateTime(Long createTime)
- {
- this.createTime = createTime;
- } public Object getSession() { return session;
- } public void setSession(Object session) { this.session = session;
- }
请使用手机扫码
//生成二维码
!function(){
var uuid =(“#uuid”).val();
varcontent;
content=“……….do?uuid=”+uuid;
//console.dir(content);(‘.pc_qr_code’).qrcode({
render:”canvas”,
width:200,
height:200,
correctLevel:0,
text:content,
background:”#ffffff”,
foreground:”black”,
src:”/logo.png”
});
setCookie(“sid”, 123, -1*60*60*1000);
keepPool();//自动循环调用
}();
java代码
ScanPool.java(存放uuid的bean)
『玖』 java大神帮帮我~~怎么优化二维码扫描软件提高识别的效率啊不求代码只求方法。万分感谢。好人一生平安。
识别有问题吗?如果你是用手机扫的话,可能是摄像头像素不够高,如果用扫瞄内枪扫的话,都没什么问容题的,我的二维码在显示器上肉眼看都比较模糊,可用防伪纸打印出来很清晰,一扫就出来了。
另外二维码只有容错率的说法,如下
// 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcodeHandler.setQrcodeErrorCorrect('M');
『拾』 现在java手机能用二维码识别软件吗飞网i码 号称可以java用,结果也是不行
智能机可以 谢谢