إن استخدام ZXING لتنفيذ الصور المدمجة لمولدات رمز الاستجابة السريعة لها قيمة مرجعية معينة ، على النحو التالي:
تتمثل الفكرة الأساسية أولاً في استخدام صورة رمز الاستجابة السريعة التي تم إنشاؤها بواسطة ZXING ، ثم اقرأ الصورة ، وإدراج الرمز فيه ، ثم إخراج الصورة بأكملها.
في المشاريع الحديثة ، نحتاج إلى إنشاء رموز QR. بعد العثور على بعض الأمثلة ، حققنا التأثير النهائي. يمكن أن يقوم رمز الاستجابة السريعة بإنشاء تنسيق الصورة (JPG ، وما إلى ذلك) أو يتم عرضه على صفحة الويب. يتم تسجيل هذه المقالة فقط ، وهناك العديد من أوجه التشابه ، لذلك أود أن أنسى ذلك. . . .
ملاحظة: بالنسبة للأدوات التي تتطلب تغليف ZXING ، فإن العملية العامة هي قراءة الصور المضمنة ، وتحويل المحتوى إلى رمز الاستجابة السريعة ، وتضمين الصور في رمز الاستجابة السريعة ، وإنتاج الصور.
هنا هو الرمز الكامل:
استيراد java.awt.basicsstroke ؛ استيراد java.awt.color ؛ استيراد java.awt.graphics2d ؛ استيراد java.awt.image ؛ image java.awt.shape java.awt.image.bufferedImage ؛ استيراد java.io.file ؛ استيراد java.io.ioException ؛ استيراد java.util.hashmap ؛ استيراد java.util.map ؛ import javax.imageio.imageio com.google.zxing.multiformatwriter ؛ استيراد com.google.zxing.writerexception ؛ استيراد com.google.zxing.common.bitmatrix ؛ استيراد com.google.zxing ثابت نهائي int image_width = 80 ؛ int static int int private image_half_width = image_width / 2 ؛ Private Static Final Frame_Width = 2 ؛ // رمز QR اكتب خاصًا متعدد الأشكال multiformatwriter mutiWriter = جديد متعدد الأشكال () ؛ public static void main (string [] args) {try {// bitmatrix bitmatrix = multiformatwriter.encode (content ، barcodeformat.qr_code ، 400 ، 400 ، hints) ؛ string content = "13400000000" ؛ if (! imageio.write (Image ، "JPG" ، ملف جديد ("F: //2122.jpg"))) {رمي iOexception جديد ("لا يمكن كتابة صورة للتنسيق") ؛ } /** // تبديل الكود أعلاه هنا واستخدم الدفق لقراءته في OutputStream OS = reposit.getOutputStream () ؛ if (! imageio.write (صورة ، "JPG" ، OS)) {رمي iOexception جديد ("لا يمكن كتابة صورة للتنسيق") ؛ } **/} catch (writerexception e) {E.PrintStackTrace () ؛} catch (ioException e) {// todo catch catch blocke.printstacktrace () ؛}} private bufferedimage tobuffuredimage (matrix bitmatrix) matrix.getheight () ؛ bufferedImage صورة = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ لـ (int x = 0 ؛ x <width ؛ x ++) {for (int y = 0 ؛ y <height ؛ y ++) Image ؛} private static bufferedimage genbarcode (محتوى السلسلة ، عرض int ، ارتفاع int ، سلسلة srcimagepath) يلقي Orderexception ، ioException {// اقرأ المصدر scaleidimage 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") ؛ // Content Proving Hint.put (Encodehinttype.error_correction ، errorcorrectionlevel.h) ؛ // error lister hint.put (encodehinttype.margin ، 1) ؛ // قم بتعيين عرض المساحة الفارغة للحدود الخارجية لرمز QR // قم بإنشاء مصفوفة Bitmatrix QR من رمز QR = 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> halfw - image_half_width && x <halfw+half_width && image_half_width) {pixels [y * width + x] = srcpixels [x - halfw + image_half_width] [y - halfh + image_half_width] ؛ }. - Frame_width && x <halfw + image_half_width + frame_width && y> halfh - image_half_width - frame_width && y <halfh + image_half_width + frame_width) || && y> halfh - image_half_width - frame_width && y <halfh - image_half_width + frame_width) || + image_half_width + frame_width)) {pixels [y * width + x] = 0xffffffff ؛ } آخر {// هنا يمكنك تعديل لون رمز الاستجابة السريعة ، ويمكنك صياغة ألوان رمز الاستجابة السريعة والخلفية على التوالي ؛ وحدات البكسل [y * width + x] = matrix.get (x ، y)؟ 0xFF000000: 0xffffffff ؛ }}} صورة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ Image.getRaster (). إرجاع الصورة ؛ } /** * قم بتوسيع نطاق الصورة الأصلية التي تم تمريرها عن طريق الارتفاع والعرض لإنشاء رمز يفي بالمتطلبات * * param srcimagefile * عنوان ملف المصدر * param الارتفاع * ارتفاع الهدف * param width * width target * param hasfiller * هل من الضروري ملء المقياس عندما يكون المقياس غير صحيح: صحيح هو ملء في. خطأ لا يملأ. * throws ioException */ مقياس bufferedimage الثابت الخاص (سلسلة srcimagefile ، ارتفاع int ، عرض int ، hasfiller المنطقية) يلقي ioexception {double ratio = 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) {// ملء صورة bufferedImage = جديد bufferedImage (العرض ، الارتفاع ، bufferedImage.type_int_rgb) ؛ graphics2d graphic = image.creategraphics () ؛ graphic.setColor (color.pink) ؛ Graphic.FillRect (10 ، 10 ، العرض ، الارتفاع) ؛ Graphic.DrawRect (100 ، 360 ، العرض ، الارتفاع) ؛ if (width == destimage.getWidth (null)) {graphic.drawImage (destimage ، 0 ، (height - destimage.getheight (null)) / 2 ، destimage.getwidth (null) ، destimage.getheight (null) ، color.white ، null) ؛ شكل الشكل = جديد RoundRecTangle2d.float (0 ، (الارتفاع - destimage.getheight (null)) / 2 ، العرض ، العرض ، 20 ، 20) ؛ Graphic.SetStroke (New BasicStroke (5F)) ؛ graphic.draw (الشكل) ؛ } آخر {graphic.drawImage (destimage ، (العرض - destimage.getWidth (null)) / 2 ، 0 ، destimage.getwidth (null) ، destimage.getheight (null) ، color.hite ، null) ؛ شكل الشكل = جديد RoundRecTangle2d.float ((العرض - destimage.getWidth (null)) / 2 ، 0 ، العرض ، العرض ، 20 ، 20) ؛ Graphic.SetStroke (New BasicStroke (5F)) ؛ graphic.draw (الشكل) ؛ } graphic.dispose () ؛ Destimage = صورة ؛ } العودة (bufferedImage) Destimage ؛ }}ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.