هناك العديد من حزم جرة التطوير لتوليد Java من رموز QR. على سبيل المثال ، تم تطوير Zxing بواسطة Google. هنا أستخدم حزمة تطوير Zxing لتنفيذها. في العديد من المشاريع ، نحتاج إلى إنشاء رموز QR ديناميكيًا لتزويدها بالمستخدمين ، حتى يتمكن المزيد من الأشخاص من تجربة تطبيقاتهم جيدًا من خلال رموز QR.
تم نشر الرمز أدناه ، وقد تم اختباره ويمكنك نسخ الكود مباشرة للاستخدام:
حققت أخيرا النتائج:
Java يولد رمز الاستجابة السريعة
الرمز كما يلي:
استيراد java.awt.color ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.awt java.io.unsupportedencodingexception ؛ استيراد java.util.hashmap ؛ استيراد java.util.map ؛ استيراد javax.imageio.imageio ؛ استيراد com.google.zxing.barcodeformat ؛ استيراد com.google.zxing.encodehinttype ؛ import com.google.zxing.multiformatwriter ؛ import com.google.zxing.writerexception ؛ import com.google.zxing.common.bitmatrix ؛ import com.zxing.qxing.qoder الفئة العامة MatrixToImageWriter {private static Final int image_width = 100 ؛ int static int private int image_height = 100 ؛ int static int int private image_half_width = image_width / 2 ؛ Private Static Final Frame_Width = 2 ؛ خاص static multiformatwriter mutiWriter = جديد متعدد الأشكال () ؛ encode public static void (محتوى السلسلة ، عرض int ، ارتفاع int ، سلسلة srcimagepath ، سلسلة destimagepath) {try {imageio.write (genbarcode (المحتوى ، العرض ، الارتفاع ، srcimagepath) ، "JPG" ، ملف جديد (DestImagePath)) ؛ } catch (ioException e) {E.PrintStackTrace () ؛ } catch (writerexception e) {E.PrintStackTrace () ؛ }} private static static bufferedimage genbarcode (محتوى السلسلة ، عرض int ، ارتفاع int ، سلسلة srcimagepath) يلقي writerexception ، ioException {bufferedImage ScaleImage = Scale (srcimagepath ، image_width ، image_height ، true) ؛ int [] [] srcpixels = new int [image_width] [image_height] ؛ لـ (int i = 0 ؛ i <scaleimage.getWidth () ؛ i ++) {for (int j = 0 ؛ j <scaleImage.getheight () ؛ j ++) {srcpixels [i] [j] = scaleImage.getrgb (i ، j) ؛ }} map <EncodeHintType ، Object> nint = new HashMap <EncodeHintType ، Object> () ؛ Hint.put (Encodehinttype.character_set ، "UTF-8") ؛ تلميح. // إنشاء رمز QR bitmatrix مصفوفة = mutiWriter.encode (المحتوى ، barcodeformat.qr_code ، العرض ، الارتفاع ، تلميح) ؛ // تحويل المصفوفة ثنائية الأبعاد إلى صفيف بكسل أحادي البعد int halfw = matrix.getWidth () / 2 ؛ int halfh = matrix.getheight () / 2 ؛ int [] pixels = new int [width * height] ؛ لـ (int y = 0 ؛ y <matrix.getheight () ؛ y ++) {for (int x = 0 ؛ x <matrix.getWidth () ؛ x ++) {// اللون في الزاوية العلوية اليسرى ، اضبط نطاق اللون واللون وفقًا لاحتياجاتك إذا (x> 0 && x <170 && int colorint = color.getrgb () ؛ وحدات البكسل [y * width + x] = matrix.get (x ، y)؟ ColorInt: 16777215 ؛ } // اقرأ الصورة الأخرى إذا (x> halfw - image_half_width && x <halfw + image_half_width && y> halfh - image_half_width && y <halfh + image_half_width) {pixels image_half_width] ؛ } آخر إذا ((x> halfw - image_half_width - frame_width && x <halfw - image_half_width + frame_width && y> halfh - image_half_width - frame_width && x <halfw + image_half_width + frame_width && image_half_width - frame_width && y <halfh - image_half_width + frame_width) || ( image_half_width + frame_width && y> halfh + image_half_width - frame_width && y <halfh + image_half_width + frame_width)) {pixels [y * width + x] = 0xfffffffffff ؛ // تشكيل حدود حول الصورة} آخر {// QR Code Color int num1 = (int) (50 - (50.0 - 13.0) / matrix.getheight () * (y + 1)) ؛ int num2 = (int) (165 - (165.0 - 72.0) / matrix.getheight () * (y + 1)) ؛ int num3 = (int) (162 - (162.0 - 107.0) / matrix.getheight () * (y + 1)) ؛ لون اللون = لون جديد (num1 ، num2 ، num3) ؛ int colorint = color.getrgb () ؛ // هنا يمكنك تعديل لون رمز الاستجابة السريعة ، ويمكنك صياغة رمز الاستجابة السريعة وألوان الخلفية على التوالي ؛ وحدات البكسل [y * width + x] = matrix.get (x ، y)؟ ColorInt: 16777215 ؛ // 0x00000: 0xffffff}}} صورة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ Image.getRaster (). إرجاع الصورة ؛ } مقياس BufferedImage الخاص الخاص (سلسلة srcimagefile ، ارتفاع int ، عرض int ، hasfiller المنطقية) يلقي ioException {نسبة مزدوجة = 0.0 ؛ // ملف المقياس = ملف جديد (srcimageFile) ؛ bufferedImage srcimage = imageio.read (file) ؛ صورة destimage = srcimage.getScaledInstance (العرض ، الارتفاع ، bufferedImage.scale_smooth) ؛ // حساب المقياس if ((srcimage.getheight ()> الارتفاع) || (srcimage.getWidth ()> العرض)) {if (srcimage.getheight ()> srcimage.getWidth ()) {ratio = (new integer (height)). } else {ratio = (new integer (width)). doublevalue () / srcimage.getWidth () ؛ } affinetransformop op = new AffinetRansformop (AffinetRansform.getScaleinstance (نسبة ، نسبة) ، فارغة) ؛ destimage = op.filter (srcimage ، null) ؛ } if (hasfiller) {// filling bufferedimage image = new BufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ graphics2d graphic = image.creategraphics () ؛ graphic.setColor (color.hite) ؛ Graphic.FillRect (0 ، 0 ، العرض ، الارتفاع) ؛ if (width == destimage.getwidth (null)) graphic.drawimage (destimage ، 0 ، (height - destimage.getheight (null)) / 2 ، destimage.getwidth (null) ، destimage.getheight (null) ، color.white ، null) ؛ else graphic.drawimage (destimage ، (width - destimage.getwidth (null)) / 2 ، 0 ، destimage.getwidth (null) ، destimage.getheight (null) ، color.white ، null) ؛ Graphic.Dispose () ؛ Destimage = صورة ؛ } العودة (bufferedImage) Destimage ؛ } يرمي الفراغ الثابت العام (سلسلة [] args) UnsupportedEncodingException {// بدوره ، المحتوى (غير مدعوم باللغة الصينية) ، العرض ، الطول ، مسار الأيقونة الأوسط ، مسار التخزين MatrixToImagwriter.encode ("http://www.baidu.com/" ، 512 ، 512 ، "D: //2013-01.jpg") ؛ }}يمكنك الرجوع إلى الموضوع: كود QR Java للتعلم
ما سبق هو كل شيء عن هذه المقالة ، مما يساعدك على تصميم رمز الاستجابة السريعة الخاصة بك.