L'utilisation de ZXing pour implémenter des images embarquées de générateurs de code QR a une certaine valeur de référence, comme suit:
L'idée de base est d'utiliser d'abord l'image du code QR généré par ZXing, puis de lire l'image, d'insérer l'icône, puis de sortir l'image entière.
Dans les projets récents, nous devons générer des codes QR. Après avoir trouvé quelques exemples, nous avons atteint l'effet final. Le code QR peut générer un format d'image (JPG, etc.) ou être affiché sur la page Web. Cet article n'est enregistré que, et il y a de nombreuses similitudes, donc je voudrais l'oublier. . . .
Remarque: Pour les outils qui nécessitent un emballage zxing, le processus général est de lire des images intégrées, de convertir le contenu en code QR, d'intégrer les images en code QR et de produire des images.
Voici le code complet:
Importer java.awt.basicStroke; import java.awt.color; import java.awt.graphics2d; import java.awt.image; image java.awt.shape; import java.awtangle2d; java.awt.image.bufferedImage; import java.io.file; import java.io.ioexception; import java.util.hashmap; import java.util.map; import javax.imageio.imageio; import com.google.zxing.barcodeformat; import com.google.zxing.endehittype; com.google.zxing.multiformatwriter; import com.google.zxing.writerexception; import com.google.zxing.common.bitmatrix; import com.google.zxing.qrcode.decoder.errorcorrectionLevel; public class zxing {private static int Black = 0xFF 0xFFFFFFFF; // Général privé static final int image_width = 80; private static final int image_half_width = image_width / 2; private static final int trame_width = 2; // QR Code écrivez privé MultiformatWriter MutiWriter = new MultiformatWriter (); public static void main (String [] args) {try {// bitmatrix bitmatrix = multiformatwriter.encode (contenu, barcodeformat.qr_code, 400, 400, hints); string contenu = "1340000000000"; // QR Code de contenu Bufferedimage Image = Genbarcode (Content, 400, 400, "F: //aMazed.png"); if (! ImageIo.write (image, "jpg", nouveau fichier ("f: //2122.jpg"))) {throw new ioException ("ne pourrait pas écrire une image de format"); } / ** // Échangez le code ci-dessus ici et utilisez le flux pour le lire dans la page OutputStream OS = Response.getOutputStream (); if (! ImageIo.write (image, "jpg", os)) {throw new ioException ("ne pourrait pas écrire une image de format"); } ** /} catch (witherexception e) {e.printStackTrace ();} catch (ioException e) {// todo Generated Catch Blocke.printStackTrace ();}} private BufferedImage tobufferedImage (Bitmatrix Matrix) {int width = matrix.getwidth (); inight = matrix.getheight (); bufferedImage Image = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); pour (int x = 0; x <width; x ++) {for (int y = 0; y <height; y ++) {image.setrgb (x, y, matrix.get (x, y)? BufferedImage GenBarCode (String Content, int largeth, int hauteur, String srciMagePath) lève WriteRereXception, ioException {// Lire l'image source 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"); // Content Encoding Hint.put (EncodeHintType.Error_Correction, ErrorCorrectionLevel.h); // Error Level Hint.put (EncodeHintType.Margin, 1); // Définissez la largeur de la zone vierge de la bordure extérieure du code QR // Générez le code QR BitMatrix Matrix = MutiWriter.encode (contenu, BarcodeFormat.qr_code, largeur, hauteur, indice); // Convertir la matrice 2D 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 ++) {for (int x = 0; x <matrix.getWidth (); x ++) {// lire l'image if (x> halfw - image_half_width && x <halfw + image_half_width &&> halfh - image_half_width & & g <h + Image_half_width) {pixels [y * width + x] = srcpixels [x - halfw + image_half_width] [y - halfh + image_half_width]; } // Formez une bordure autour de l'image else if ((x> halfw - image_half_width - frame_width && x <halfw - image_half_width + frame_width && y> halfh - image_half_width - frame_width && <halfh + image_half_width + frame_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) || (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)) {pixels [y * width + x] = 0xffffffff; } else {// Ici, vous pouvez modifier la couleur du code QR, et vous pouvez formuler respectivement les couleurs du code QR et de l'arrière-plan; pixels [y * largeur + x] = matrix.get (x, y)? 0xff000000: 0xffffffff; }}} BufferedImage Image = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); image.getRaster (). SetDataelements (0, 0, largeur, hauteur, pixels); Image de retour; } / ** * Échelle l'image d'origine passée par hauteur et largeur pour générer une icône qui répond aux exigences * * @param srciMageFile * Adresse du fichier source * @param height * hight * @param width * width cible * @param Hasfiller * est-il nécessaire de remplir lorsque l'échelle est incorrecte: true est rempli; Faux n'est pas rempli; * @Throws IOException * / Échelle de tampon statique privée (String SrciageFile, Int Height, Int Width, Boolean Hasfiller) lève IOException {Double Ratio = 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) {// Remplissant BufferedImage Image = new BufferedImage (largeur, hauteur, bufferedImage.type_int_rgb); Graphics2d graphic = image.creategraphics (); graphic.setColor (Color.pink); Graphic.Fillrect (10, 10, largeur, hauteur); Graphic.Drawrect (100, 360, 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); Forme de forme = nouveau RoundRectangle2d.float (0, (hauteur - Destimage.getheight (null)) / 2, largeur, largeur, 20, 20); Graphic.SetStroke (New BasicStroke (5F)); Graphic.Draw (forme); } else {Graphic.DrawImage (Destimage, (Width - Destimage.getWidth (null)) / 2, 0, Destimage.getWidth (null), Destriage.getheight (null), Color.White, Null); Forme de forme = new RoundRectangle2d.float ((largeur - devanage.getWidth (null)) / 2, 0, largeur, largeur, 20, 20); Graphic.SetStroke (New BasicStroke (5F)); Graphic.Draw (forme); } graphic.dispose (); DESTIMAGE = Image; } return (bufferedImage) Destrimage; }}Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.