// 内网通扫码签到的二维码识别
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.google.gson.Gson;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
public class Test {
private static final String fromDir = "/mnt/shared/qrcodes";
private static final String toDir = "/mnt/shared/processedQRCodes";
private static final String PNG = ".png";
public static void main(String[] args) {
File dir = new File(fromDir);
if(dir.exists() && dir.isDirectory()){
File[] files = dir.listFiles();
for(File f:files){
if(f.isFile()&&f.getName().toLowerCase().endsWith(PNG)){
String res = decode(f);
System.out.println(res);
if(StringUtils.isNotBlank(res)){
// move to processed dir
boolean moved = move(f,new File(toDir+File.separator+f.getName().substring(0,f.getName().indexOf("."))+"_"+res+PNG));
System.out.println(moved==true?"Moved to processed dir.":"Can't move file: "+f.getAbsolutePath());
}
}
}
}
encode("http://baike.baidu.com/link?url=BP7l08ZZZa7HBLL0g4RCuVsN3BlNbuokYRKCuUk8ZexPDjxSVNMXQq_08WI1xz5EUAjVrce4qgSisos9K-mq-q");
}
private static boolean move(File f,File toDir){
return f.renameTo(toDir);
}
/**
* 解析图像
*/
public static String decode(File file) {
BufferedImage image;
String res = null;
try {
image = ImageIO.read(file);
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
// hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
String resultStr = result.getText();
// System.out.println(resultStr);
String t = resultStr.substring(resultStr.indexOf("?")+3,resultStr.length());
String base64 = new String(Base64.decodeBase64(t));
// System.out.println(t);
System.out.print(base64+" ---> ");
DecodeResult decodeResult = new Gson().fromJson(base64,DecodeResult.class);
res = decodeResult.getD();
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
return res;
}
public static void encode(String content){
int width = 400; // 图像宽度
int height = 400; // 图像高度
String format = "png";// 图像类型
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try{
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
Path path = FileSystems.getDefault().getPath(toDir, "code.png");
MatrixToImageWriter.writeToPath(bitMatrix, format, path);// 输出图像
}catch(Exception e){
e.printStackTrace();
}
}
}
class DecodeResult{
private String a;
private String b;
private String c;
private String d;
private String v;
public String getA() {
return a;
}
public void setA(String a) {
this.a = a;
}
public String getB() {
return b;
}
public void setB(String b) {
this.b = b;
}
public String getC() {
return c;
}
public void setC(String c) {
this.c = c;
}
public String getD() {
return d;
}
public void setD(String d) {
this.d = d;
}
public String getV() {
return v;
}
public void setV(String v) {
this.v = v;
}
}