import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.CharacterCodingException; import java.nio.charset.Charset; import java.nio.charset.CharsetEncoder; import java.util.Hashtable; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.*; public class QRCode { public String m_encoding="" ; public boolean m_sts= true ; public String m_ErrorInfo="" ; public String m_Output="" ; public int m_debug= 0 ; public QRCode() { this.m_encoding =""; this.m_sts=true ; this.m_ErrorInfo=""; this.m_debug=0 ; } public void setEncoding(String enc) { this.m_encoding=enc; } public void setDebug(int debug) { this.m_debug=debug; } public void makeQRCode(byte[] b,int w,int h,String filename) { m_sts=true ; String data=""; BitMatrix matrix = null; try { if (m_encoding.length()>0) data = new String(b,m_encoding); else data = new String(b,"UTF-8"); } catch (Exception e) { m_sts=false ; if (e.getMessage()==null) m_ErrorInfo="ERROR(1): UnsupportedEncoding"; else m_ErrorInfo="ERROR(1): " + e.getMessage(); if (m_debug>0) e.printStackTrace(); } if (!m_sts) return ; try { com.google.zxing.Writer writer = new MultiFormatWriter(); Hashtable hints = new Hashtable(2); if (m_encoding.length()>0) hints.put(EncodeHintType.CHARACTER_SET, m_encoding); else hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); matrix = writer.encode(data,com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints); } catch (com.google.zxing.WriterException e) { m_sts=false ; if (e.getMessage()==null) m_ErrorInfo="ERROR(2): MultiFormatWriter error "; else m_ErrorInfo="ERROR(2): " + e.getMessage(); if (m_debug>0) e.printStackTrace(); } if (!m_sts) return ; try { File file = new File(filename); MatrixToImageWriter.writeToFile(matrix, "PNG", file); m_ErrorInfo=filename; } catch (Exception e) { m_sts=false ; if (e.getMessage()==null) m_ErrorInfo="ERROR(3): Error creating file "+filename; else m_ErrorInfo="ERROR(3): " + e.getMessage(); if (m_debug>0) e.printStackTrace(); } } public String getErrInfo() { return this.m_ErrorInfo ; } public boolean getStatus() { return this.m_sts ; } public String Version() { return "1.0" ; } public static void main(String[] args) { System.out.println("QRCode v.1.0"); if (args.length < 1) { System.err.println("Usage:"); System.err.println("java QRCode file"); } else { String outFile = args[args.length-1]; QRCode qr=new QRCode(); qr.makeQRCode("Test".getBytes(),100,100,outFile) ; if (qr.getStatus()) { System.out.println("File "+outFile+ " created"); } else { System.out.println(qr.getErrInfo()); } } } }