1. دعم QRCODE و ZXING CODE QR CODE وتحليله ؛
2. QRCODE يولد رموز QR ويدعم إضافة الصور ، على النحو التالي:
حزمة شائعة. استيراد java.awt.color ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ image استيراد jp.sourceforge.qrcode.qrcodedecoder ؛ استيراد jp.sourceforge.qrcode.exception.decodingFailedException ؛ استيراد com.google.zxing.barcodeformat ؛ استيراد com.google.zxing.binarizer ؛ استيراد com.google.zxing.binarybitmap ؛ import com.google.zxing.encodehinttype ؛ import com.google.zxing.luminancesource ؛ com.google.zxing.notfoundException ؛ import com.google.zxing.writerexception ؛ import com.google.zxing.common.bitmatrix ؛ import com.google.zxing.common.hybridbinarizer ؛ import com.swetake.util.qrcode ؛ /** * فئة أداة توليد رمز QR * Author Cloud * data 2016-12-15 * QRCode */public class qrcodeutil {// QR Code Color Private Static Final int Black = 0xFF000000 ؛ // QR Code Color Private Static Final Int White = 0xffffffff ؛ /** * <span style = "font-size: 18px ؛ font-weight: blod ؛"> zxing method تقوم بإنشاء رمز QR </span> * param text <a href = "javaScript: void () ؛" rel = "خارجي nofollow"> محتوى رمز QR </a>* param عرض رمز QR عرض* param ارتفاع رمز QR* param outputpath code QR Generation وحفظ المسار* param meactype QR code format*/ public static zxingCodeCreate (string text ، int quep ، stringpath ، HashMap <EncodeHintType ، string> () ؛ // قم بتعيين الحرف المشفر تعيين His.put (EncodeHintType.Character_set ، "UTF-8") ؛ حاول {// 1. إنشاء رمز QR bitmatrix encode = new MultiformatWriter (). encode (النص ، barcodeformat.qr_code ، العرض ، الارتفاع ، له) ؛ // 2. احصل على عرض رمز الاستجابة السريعة والارتفاع int codewedth = encode.getWidth () ؛ int codeheight = encode.getheight () ؛ // 3. ضع رمز الاستجابة السريعة في صورة BufferedImage المخزنة = CodeWidth ، Codeheight ، BufferedImage.type_int_rgb) ؛ لـ (int i = 0 ؛ i <codewidth ؛ i ++) {for (int j = 0 ؛ j <codeheight ؛ j ++) {// 4. حلقة لإصلاح محتوى رمز الاستجابة السريعة في Image Image.setRgb (i ، j ، encode.get (i ، j)؟ Black: White) ؛ }} File OutputImage = ملف جديد (OutputPath) ؛ // إذا لم تكن الصورة موجودة ، فقم بإنشاء الصورة إذا (! outputImage.exists ()) outputImage.createNewFile () ؛ // 5. اكتب رمز الاستجابة السريعة إلى صورة Imageio.write (الصورة ، التصوير ، الإخراج) ؛ } catch (writerexception e) {E.PrintStackTrace () ؛ System.out.println ("فشل توليد رمز QR") ؛ } catch (ioException e) {E.PrintStackTrace () ؛ system.out.println ("فشل تغيير صورة رمز QR") ؛ } ستر multiformatreader orportreader = جديد متعدد الأشكال () ؛ نتيجة الكائن = فارغة ؛ حاول {file file = ملف جديد (AnalyzePath) ؛ if (! file.exists ()) {return "رمز QR غير موجود" ؛ } ImageImage Image = imageio.read (file) ؛ مصدر luminancesource = new luminancesourceutil (صورة) ؛ binarizer binarizer = new hybridbinarizer (source) ؛ binarybitmap binarybitmap = جديد binarybitmap (binarizer) ؛ تلميحات الخريطة = new hashmap () ؛ Hints.put (Encodehinttype.character_set ، "UTF-8") ؛ النتيجة = formatreader.decode (binarybitmap ، تلميحات) ؛ } catch (notfoundException e) {E.PrintStackTrace () ؛ } نتيجة الإرجاع ؛ }/*** <span style = "font-size: 18px ؛ font-weight: blod ؛"> qrcode يقوم بإنشاء رمز QR </span>* param محتوى رمز QR* param imgpath code path path* publar qrate qr qrate qrate is to to to to to to to to to to logo logo do not do not in static Quit ( int الإصدار ، string logopath) {try {qrcode qrcodehandler = new qrcode () ؛ // قم بتعيين معدل ترميز الأخطاء في رمز الاستجابة السريعة ، L (7 ٪) M (15 ٪) Q (25 ٪) H (30 ٪). كلما ارتفع معدل ترميز الأخطاء ، يمكن تخزين المعلومات أقل ، ولكن كلما كانت متطلبات رمز QRCodehandler.SetQRcodeerRorcorrect أصغر. // n يمثل الرقم ، ويمثل A الحرف AZ ، ويمثل B أحرفًا أخرى qrcodehandler.setqrcodeencodeMode ('B') ؛ // الإصدار 1 عبارة عن مصفوفة 21*21 ، ولكل زيادة في الإصدار ، يتم زيادة جانبي رمز الاستجابة السريعة بمقدار 4 ؛ لذلك الإصدار 7 هو مصفوفة مع 45*45 ؛ أعلى إصدار هو 40 ، وهو مصفوفة مع 177*177 qrcodehandler.setqrcodeversion (الإصدار) ؛ // حساب الحجم وفقًا للإصدار int imgsize = 67 + 12 * (الإصدار - 1) ؛ byte [] contentBytes = content.getBytes ("GB2312") ؛ bufferedImage bufimg = جديد bufferedImage (imgsize ، imgsize ، bufferedimage.type_int_rgb) ؛ graphics2d gs = bufimg.creategraphics () ؛ Gs.SetBackground (color.hite) ؛ Gs.ClearRect (0 ، 0 ، imgsize ، imgsize) ؛ // تعيين لون الصورة> الأسود gs.setColor (color.black) ؛ // set set Offset not to set قد يتسبب في تحليلية خطأ int pixoff = 2 ؛ // إخراج محتوى> رمز QR if (contentBytes.length> 0 && contentBytes.length <130) {boolean [] [] codeout = qrcodehandler.calqrcode (contentBytes) ؛ لـ (int i = 0 ؛ i <codeout.length ؛ i ++) {for (int j = 0 ؛ j <codeout.length ؛ j ++) {if (codeout [j] [i]) {gs.fillRect (j * 3+pixoff ، i * 3+pixoff ، 3 ، 3) ؛ }}}} آخر {system.err.println ("qrcode content bytes length =" + contentBytes.length + "not in [0،130].") ؛ } /* حدد ما إذا كنت بحاجة إلى إضافة صورة شعار* / if (logopath! = null) {file icon = new file (logopath) ؛ if (icon.exists ()) {int width_4 = imgsize / 4 ؛ int width_8 = width_4 / 2 ؛ int height_4 = imgsize / 4 ؛ int height_8 = height_4 / 2 ؛ Image IMG = Imageio.Read (icon) ؛ Gs.DrawImage (img ، width_4 + width_8 ، height_4 + height_8 ، width_4 ، height_4 ، null) ؛ Gs.Dispose () ؛ bufimg.flush () ؛ } آخر {system.out.println ("خطأ: صورة تسجيل الدخول غير موجودة!") ؛ }} gs.dispose () ؛ bufimg.flush () ؛ // إنشاء ملف ملف رمز QR imgfile = ملف جديد (imgpath) ؛ if (! imgfile.exists ()) imgfile.createNewFile () ؛ // احصل على الصورة من سلسلة الصور التي تم إنشاؤها imgtype = imgpath.substring (imgpath.lastindexof (".") + 1 ، imgpath.length ()) ؛ // إنشاء رمز QR qrcode imageio.write (bufimg ، imgtype ، imgfile) ؛ } catch (استثناء e) {E.PrintStackTrace () ؛ }}/*** <span style = "font-size: 18px ؛ font-weight: blod ؛"> QRCode QR Code Analysis </span>* param codepath QR code path* regurn return result*/public static string qrcodeanalyze (string codepath) {file imagefile = new file (codepath) ؛ BufferedImage bufimg = null ؛ سلسلة decodedData = فارغة ؛ حاول {if (! imagefile.exists ()) إرجاع "رمز QR غير موجود" ؛ bufimg = imageio.read (imageFile) ؛ QRCODEDECODER DECODER = QRCODEDECODER () جديد ؛ decodedData = سلسلة جديدة (decoder.decode (new ImageUtil (bufimg)) ، "GB2312") ؛ } catch (ioException e) {system.out.println ("error:" + E.GetMessage ()) ؛ E.PrintStackTrace () ؛ } catch (decodingFailedException dfe) {system.out.println ("error:" + dfe.getMessage ()) ؛ dfe.printstacktrace () ؛ } إرجاع decodeddata ؛ }} 3. آخر رمز اختبار النشر: اختبار الحزمة ؛ استيراد java.awt.image.bufferedImage ؛ استيراد java.io.inputstream ؛ استيراد java.net.url ؛ استيراد javax.imageio.imageio ؛ استيراد common.imageutil ؛ استيراد common.qrcodeutil ؛ استيراد jp.sourceforge.qrcode.qrcodedecoder ؛ /*** فئة اختبار توليد رمز QR* Author Cloud* data 2016-11-21* QRCODETEST* /الفئة العامة QRCODETEST {public static void main (string [] args) remons qrcodeutil.qrcodecreate ("http://blog.csdn.net/u014266877" ، "e: //qrcode.jpg" ، 15 ، "e: //icon.png") ؛ *//*** QRCode QR Code Analysis Test* String qrcodeanalyze = qrcodeutil.qrcodeanalyze ("e: //qrcode.jpg") ؛ *//*** اختبار توليد رمز QR ZxingCode* qrcodeutil.zxingCodecreate ("http://blog.csdn.net/U014266877" ، 300 ، 300 ، "E: //zxingcode.jpg" ، "JPG") ؛ *//*** ZxingCode QR Code Analysis* String zxingAnalyze = qrcodeutil.zxingCodeanalyze ("e: //zxingcode.jpg") .ToString () ؛ */ system.out.println ("النجاح") ؛ }}