Il existe de nombreux packages de bocaux de développement pour la génération Java de codes QR. Par exemple, ZXing est développé par Google. Ici, j'utilise le package de développement de ZXing pour l'implémenter. Dans de nombreux projets, nous devons générer dynamiquement des codes QR pour les fournir aux utilisateurs, afin que davantage de personnes puissent bien ressentir leurs applications via des codes QR.
Le code est publié ci-dessous, et il a été testé et vous pouvez copier le code directement à utiliser:
Enfin obtenu les résultats:
Java génère du code QR
Le code est le suivant:
Importer java.awt.color; import java.awt.graphics2d; import java.awt.image; import java.awt.geom.affinetransform; import java.awt.image.affinetransformop; import java.awt.image.bufferedImage; import java.io.file; import java.io.io. java.io.unsupportEnCoDingException; import java.util.hashmap; import java.util.map; import javax.imageio.imageio; import com.google.zxing.barcodeformat; import com.google.zxing.encodehintype; import com.google.zxing.multiformatwriter; import com.gomon.zxing.writexception; import com.google.zxing.common.bitmatrix; importer; com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; classe publique MatrixToimageWriter {private static final int image_width = 100; private static final int image_height = 100; private static final int image_half_width = image_width / 2; private static final int trame_width = 2; Multiformatwriter statique privé MutiWriter = new MultiformatWriter (); Public Static Void Encode (String Content, int width, int height, String srciMagePath, String DestimagePath) {try {imageo.write (genbarcode (contenu, largeur, hauteur, srciMagePath), "jpg", nouveau fichier (DestrimagePath)); } catch (ioException e) {e.printStackTrace (); } catch (writeRexception e) {e.printStackTrace (); }} Private Static BufferedImage GenBarcode (String Content, int largeth, int hauteur, String srciMagePath) lève WriteRException, ioException {bufferedImage ScaleImage = SCALE (srciMagePath, image_width, image_height, true); int [] [] srcPixels = new int [image_width] [image_height]; for (int i = 0; i <scaleImage.getWidth (); i ++) {for (int j = 0; j <scaleImage.getHeight (); j ++) {srcpixels [i] [j] = scaleImage.getrgb (i, j); }} Map <encodeHintType, objet> hint = new hashmap <encodeHintType, objet> (); hint.put (encodeHintType.Character_set, "UTF-8"); hint.put (encodeHintType.Error_Correction, errorCorrectionLevel.h); // Générer le code QR BitMatrix Matrix = MutiWriter.Encode (Content, BarcodeFormat.QR_CODE, Width, Height, Hint); // Convertir la matrice bidimensionnelle en un tableau de pixels unidimensionnel int halfw = matrix.getWidth () / 2; int halfh = matrix.getheight () / 2; int [] pixels = new int [largeur * hauteur]; pour (int y = 0; y <matrix.getHeight (); y ++) {pour (int x = 0; x <matrix.getWidth (); x ++) {// la couleur dans le coin supérieur gauche, ajustez la gamme de couleurs et la couleur selon vos besoins si (x> 0 && x <170 && y> 0 && y <170) {Color = new Color (231, 144, 56); int colorint = color.getrgb (); pixels [y * largeur + x] = matrix.get (x, y)? Colorrint: 16777215; } // Lire l'image else if (x> halfw - image_half_width && x <halfw + image_half_width && y> halfh - image_half_width && y <halfh + image_half_width) {pixels [y * width + x] = srcpixel Image_half_width]; } else if ((x> halfw - 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) || (x> halfw + image_half x <halfw + image_half_width + frame_width && y> halfw - image_half_width - frame_width && y <halfh + image_half_width + frame_width) || (x> halfw - image_half_width - frame_width & y <halfw + image_half_width + frame_ Image_half_width - frame_width && y <halfh - image_half_width + frame_width) || (x> halfw - image_half_width - frame_width && <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] = 0xffffffff; // Formez une bordure autour de l'image} else {// 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)); Couleur couleur = nouvelle couleur (num1, num2, num3); int colorint = color.getrgb (); // Ici, vous pouvez modifier la couleur du code QR, et vous pouvez formuler respectivement le code QR et les couleurs d'arrière-plan; pixels [y * largeur + x] = matrix.get (x, y)? Colorrint: 16777215; // 0x00000: 0xffffff}}} BufferedImage Image = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); image.getRaster (). SetDataelements (0, 0, largeur, hauteur, pixels); Image de retour; } Échelle privée de tampon statique (chaîne srciagefile, hauteur int, largeur int, booléen hasfiller) lève ioException {double rapport = 0,0; // échelle fichier = nouveau fichier (srciagefile); BufferedImage srciMage = imageo.read (fichier); Image Destimage = srciage.getScaledInstance (largeur, hauteur, bufferedImage.scale_smooth); // calculer l'échelle if ((srciage.getheight ()> height) || (srciage.getWidth ()> width)) {if (srciage.getheight ()> srciage.getWidth ()) {ratio = (new Integer (hauteur)). DoubleValue () / Srcimage.GetHetheight (); } else {ratio = (new Integer (width)). doubleValue () / srciage.getWidth (); } AffinetRansformOp OP = new AffineTransFormop (affinetRansform.getScaleInstance (rapport, ratio), null); Destimage = op.filter (srcimage, null); } if (HasFiller) {// Remplissage de BufferedImage Image = new BufferedImage (largeur, hauteur, tampondImage.type_int_rgb); Graphics2d graphic = image.creategraphics (); Graphic.setColor (Color.white); Graphic.Fillrect (0, 0, largeur, hauteur); if (width == Destimage.getWidth (null)) graphic.Drawimage (Destimage, 0, (Height - Destimage.getheight (null)) / 2, Destriage.getWidth (null), destriage.getheight (null), colore.white, null); else graphic.DrawImage (Destimage, (largeur - Destimage.getWidth (null)) / 2, 0, Destimage.getWidth (null), Destimage.getheight (null), Color.white, null); graphic.dispose (); DESTIMAGE = Image; } return (bufferedImage) Destrimage; } public static void main (string [] args) lève unportdedencodingException {// à son tour, contenu (non pris en charge en chinois), largeur, longueur, chemin d'icône moyenne, chemin de stockage matrixtoimagewriter.encode ("http://www.baidu.com/", 512, "d: //logo.png", "D: //2013-01.jpg"); }}Vous pouvez vous référer au sujet: Java QR Code for Learning
Ce qui précède concerne cet article, vous aidant à concevoir votre propre code QR.