├── .idea └── vcs.xml ├── LICENSE ├── README.md ├── img ├── logo.png └── logo2.jpg ├── pom.xml ├── qrcode-core-1.1.jar └── src └── main ├── java └── org │ └── oriboy │ └── qrcode │ ├── BarcodeUtils.java │ └── QRCodeUtils.java └── resources └── font └── msyh.ttc /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # oriboy qrcode使用文档 2 | ## 介绍 3 | QRCodeUtils是基于Google提供的zxing二维码生成库而整合的一个生成或者解析二维码的工具类。 4 | BarcodeUtils是基于Google提供的zxing条形码生成库而整合的一个生成或者解析条形的工具类。 5 | ## 使用方法 6 | 将qrcode-core-1.0.jar引入项目lib库,直接调用即可。 7 | ## API说明 8 | 请查看工具类javadoc注释。 9 | 10 | 11 | ---------- 12 | 使用过程中,有任何问题可以在我的[CSDN博客](https://blog.csdn.net/qq_24091555)或者我的[Github主页](https://github.com/ToView)上留言。 13 | -------------------------------------------------------------------------------- /img/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ToView/qrcodecore/23a868a392b833ed9b5a0b3313139c27b6a84a2a/img/logo.png -------------------------------------------------------------------------------- /img/logo2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ToView/qrcodecore/23a868a392b833ed9b5a0b3313139c27b6a84a2a/img/logo2.jpg -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | org.oriboy.qrcode 8 | qrcode-core 9 | 1.1 10 | jar 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 17 | 7 18 | 7 19 | 20 | 21 | 22 | 23 | maven-assembly-plugin 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | jar-with-dependencies 33 | 34 | 35 | 36 | 37 | 38 | 39 | google zxing <--> 40 | 41 | com.google.zxing 42 | core 43 | 3.3.0 44 | 45 | 46 | com.google.zxing 47 | javase 48 | 3.3.0 49 | 50 | 51 | thumbnailator <--> 52 | 53 | net.coobird 54 | thumbnailator 55 | 0.4.8 56 | 57 | 58 | 59 | -------------------------------------------------------------------------------- /qrcode-core-1.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ToView/qrcodecore/23a868a392b833ed9b5a0b3313139c27b6a84a2a/qrcode-core-1.1.jar -------------------------------------------------------------------------------- /src/main/java/org/oriboy/qrcode/BarcodeUtils.java: -------------------------------------------------------------------------------- 1 | package org.oriboy.qrcode; 2 | 3 | import com.google.zxing.*; 4 | import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 5 | import com.google.zxing.client.j2se.MatrixToImageConfig; 6 | import com.google.zxing.client.j2se.MatrixToImageWriter; 7 | import com.google.zxing.common.BitMatrix; 8 | import com.google.zxing.common.HybridBinarizer; 9 | import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 10 | import net.coobird.thumbnailator.Thumbnails; 11 | import net.coobird.thumbnailator.geometry.Positions; 12 | 13 | import javax.imageio.ImageIO; 14 | import java.awt.*; 15 | import java.awt.image.BufferedImage; 16 | import java.io.ByteArrayOutputStream; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.nio.file.FileSystems; 21 | import java.nio.file.Path; 22 | import java.util.EnumMap; 23 | import java.util.Map; 24 | 25 | /** 26 | * @author UW-JAVA 27 | * @date 2021/3/18 15:25 28 | */ 29 | public class BarcodeUtils { 30 | 31 | /** 32 | * 条形码默认宽度 33 | */ 34 | private static final int BARCODE_WIDTH = 180; 35 | 36 | /** 37 | * 条形默认高度 38 | */ 39 | private static final int BARCODE_HEIGHT = 70; 40 | 41 | 42 | /** 43 | * 默认条形码文件格式 44 | */ 45 | private static final String FORMAT = "png"; 46 | 47 | /** 48 | * 条形条形码参数 49 | */ 50 | private static final EnumMap ENCODE_HINTS = new EnumMap<>(EncodeHintType.class); 51 | 52 | /** 53 | * 解析条形码参数 54 | */ 55 | private static final EnumMap DECODE_HINTS = new EnumMap<>(DecodeHintType.class); 56 | 57 | /** 微软雅黑常规字体库 字体库 */ 58 | private static final String BASE_FONT = "font/msyh.ttc"; 59 | 60 | static { 61 | /* 字符编码 */ 62 | ENCODE_HINTS.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 63 | /* 容错等级 L、M、Q、H 其中 L 为最低, H 为最高 */ 64 | ENCODE_HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 65 | /* 字符编码 */ 66 | DECODE_HINTS.put(DecodeHintType.CHARACTER_SET, "UTF-8"); 67 | /* 优化精度 */ 68 | DECODE_HINTS.put(DecodeHintType.TRY_HARDER, true); 69 | } 70 | 71 | private BarcodeUtils() { 72 | } 73 | 74 | /** 75 | * @title 生成条形图片 76 | * @description 根据文本内容生成默认格式的二维图图片 77 | * @param text 文本内容 78 | * @param filePath 生成图片路径 79 | * @throws WriterException 80 | * @throws IOException 81 | */ 82 | public static void create(String text, String filePath) throws WriterException, IOException { 83 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, BARCODE_WIDTH, BARCODE_HEIGHT, ENCODE_HINTS); 84 | Path path = FileSystems.getDefault().getPath(filePath); 85 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path); 86 | } 87 | 88 | /** 89 | * @title 生成条形码图片 90 | * @description 根据文本内容生成条形码图片的字节输出流 91 | * @param text 文本内容 92 | * @return 字节输出流 93 | * @throws WriterException 94 | * @throws IOException 95 | */ 96 | public static ByteArrayOutputStream create(String text) throws WriterException, IOException { 97 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, BARCODE_WIDTH, BARCODE_HEIGHT, ENCODE_HINTS); 98 | return writeToStream(bitMatrix, FORMAT); 99 | } 100 | 101 | /** 102 | * @title 生成条形码图片 103 | * @description 根据文本内容,自定义宽度高度,生成所需要的条形码图片 104 | * @param text 文本内容 105 | * @param filePath 生成图片文件路径 106 | * @param width 宽度 107 | * @param height 高度 108 | */ 109 | public static void create(String text, String filePath, int width, int height) throws WriterException, IOException { 110 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, width, height, ENCODE_HINTS); 111 | Path path = FileSystems.getDefault().getPath(filePath); 112 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path); 113 | } 114 | 115 | /** 116 | * @title 生成条形图片 117 | * @description 根据文本内容,自定义宽度高度,生成所需要的条形码图片 118 | * @param text 文本内容 119 | * @param filePath 生成图片文件路径 120 | * @param width 宽度 121 | * @param height 高度 122 | * @param onColor 条形码颜色 123 | * @param offColor 条形码背景颜色 124 | */ 125 | public static void create(String text, String filePath, int width, int height, Color onColor, Color offColor) throws WriterException, IOException { 126 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, width, height, ENCODE_HINTS); 127 | Path path = FileSystems.getDefault().getPath(filePath); 128 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path, new MatrixToImageConfig(onColor.getRGB(), offColor.getRGB())); 129 | } 130 | 131 | /** 132 | * @title 生成条形码图片 133 | * @description 根据文本内容生成条形码图片的字节输出流 134 | * @param text 文本内容 135 | * @param width 宽度 136 | * @param height 高度 137 | * @return 字节输出流 138 | */ 139 | public static ByteArrayOutputStream create(String text, int width, int height) throws WriterException, IOException { 140 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 141 | return writeToStream(bitMatrix, FORMAT); 142 | } 143 | 144 | /** 145 | * @title 生成条形码图片 146 | * @description 根据文本内容,自定义宽度高度,自定义图片格式,生成所需要的条形码图片 147 | * @param text 文本内容 148 | * @param filePath 生成图片文件路径 149 | * @param width 宽度 150 | * @param height 高度 151 | * @param format 图片格式 152 | */ 153 | public static void create(String text, String filePath, int width, int height, String format) throws WriterException, IOException { 154 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 155 | Path path = FileSystems.getDefault().getPath(filePath); 156 | MatrixToImageWriter.writeToPath(bitMatrix, format, path); 157 | } 158 | 159 | /** 160 | * @title 生成条形图片 161 | * @description 根据文本内容生成条形图片的字节输出流 162 | * @param text 文本内容 163 | * @param width 宽度 164 | * @param height 高度 165 | * @param format 自定义图片格式 166 | * @return 字节输出流 167 | */ 168 | public static ByteArrayOutputStream create(String text, int width, int height, String format) throws WriterException, IOException { 169 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 170 | return writeToStream(bitMatrix, format); 171 | } 172 | 173 | /** 174 | * @title 生成条形码图片 175 | * @description 根据文本内容,自定义宽度高度,自定义图片格式,自定义配置信息,生成所需要的条形码图片 176 | * @param text 文本内容 177 | * @param filePath 生成图片文件路径 178 | * @param width 宽度 179 | * @param height 高度 180 | * @param format 图片格式 181 | * @param hintTypes 配置信息 182 | */ 183 | public static void create(String text, String filePath, int width, int height, String format, EnumMap hintTypes) throws WriterException, IOException { 184 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes); 185 | Path path = FileSystems.getDefault().getPath(filePath); 186 | MatrixToImageWriter.writeToPath(bitMatrix, format, path); 187 | } 188 | 189 | /** 190 | * @title 生成条形码图片 191 | * @param text 文本内容 192 | * @param width 宽度 193 | * @param height 高度 194 | * @param format 图片格式 195 | * @param hintTypes 配置信息 196 | * @return 字节输出流 197 | * @throws WriterException 198 | * @throws IOException 199 | */ 200 | public static ByteArrayOutputStream create(String text, int width, int height, String format, EnumMap hintTypes) throws WriterException, IOException { 201 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes); 202 | return writeToStream(bitMatrix, format); 203 | } 204 | 205 | /** 206 | * 生成带文字的条形图片 207 | * @param text 文本内容 208 | * @param filePath 生成文件存放路径 209 | */ 210 | public static void createWithText(String text, String filePath) throws IOException, WriterException { 211 | createWidthLogo(text, BARCODE_WIDTH, BARCODE_HEIGHT, FORMAT, ENCODE_HINTS) 212 | .toFile(filePath); 213 | } 214 | 215 | 216 | /** 217 | * @title 解析条形码 218 | * @description 解析条形,获取其中文本信息 219 | * @param filePath 图片路径 220 | * @return 文本信息 221 | * @throws IOException 222 | * @throws FormatException 223 | * @throws ChecksumException 224 | * @throws NotFoundException 225 | */ 226 | public static String read(String filePath) throws IOException, FormatException, ChecksumException, NotFoundException { 227 | BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath)); 228 | return readBarcode(binaryBitmap, DECODE_HINTS).getText(); 229 | } 230 | 231 | /** 232 | * @title 解析条形码 233 | * @description 解析条形码,获取其中文本信息 234 | * @param inputStream 输入流 235 | * @return 文本信息 236 | * @throws IOException 237 | * @throws FormatException 238 | * @throws ChecksumException 239 | * @throws NotFoundException 240 | */ 241 | public static String read(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException { 242 | BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream); 243 | return readBarcode(binaryBitmap, DECODE_HINTS).getText(); 244 | } 245 | 246 | /** 247 | * @title 解析条形码 248 | * @description 解析条形码,获取其中文本信息 249 | * @param filePath 图片路径 250 | * @param decodeHints 解析配置 251 | * @return 文本信息 252 | * @throws FormatException 253 | * @throws ChecksumException 254 | * @throws NotFoundException 255 | * @throws IOException 256 | */ 257 | private static String read(String filePath, Map decodeHints) throws FormatException, ChecksumException, NotFoundException, IOException { 258 | BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath)); 259 | return readBarcode(binaryBitmap, decodeHints).getText(); 260 | } 261 | 262 | /** 263 | * @title 解析条形码 264 | * @description 解析条形码,获取其中文本信息 265 | * @param inputStream 输入流 266 | * @return 文本信息 267 | * @throws IOException 268 | * @throws FormatException 269 | * @throws ChecksumException 270 | * @throws NotFoundException 271 | */ 272 | public static String read(InputStream inputStream, Map decodeHints) throws IOException, FormatException, ChecksumException, NotFoundException { 273 | BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream); 274 | return readBarcode(binaryBitmap, decodeHints).getText(); 275 | } 276 | 277 | 278 | /** 279 | * @title 生成BitMatrix 280 | * @param text 文本内容 281 | * @param barcodeFormat 条形码格式 282 | * @param width 宽度 283 | * @param height 高度 284 | * @param hintTypes 配置 285 | * @return BitMatrix 286 | * @throws WriterException 287 | */ 288 | private static BitMatrix createBitMatrix(String text, BarcodeFormat barcodeFormat, int width, int height, EnumMap hintTypes) throws WriterException { 289 | MultiFormatWriter writer = new MultiFormatWriter(); 290 | return writer.encode(text, barcodeFormat, width, height, hintTypes); 291 | } 292 | 293 | /** 294 | * 转成字符输出流 295 | * @param bitMatrix bitMatrix 296 | * @param format 图片格式 297 | * @return 298 | * @throws IOException 299 | */ 300 | private static ByteArrayOutputStream writeToStream(BitMatrix bitMatrix, String format) throws IOException { 301 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 302 | MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream); 303 | return outputStream; 304 | } 305 | 306 | /** 307 | * 解析条形码 308 | * @param binaryBitmap 条形码图类 309 | * @return 解析信息 310 | * @throws FormatException 311 | * @throws ChecksumException 312 | * @throws NotFoundException 313 | */ 314 | private static Result readBarcode(BinaryBitmap binaryBitmap, Map decodeHints) throws FormatException, ChecksumException, NotFoundException { 315 | MultiFormatReader reader = new MultiFormatReader(); 316 | return reader.decode(binaryBitmap, decodeHints); 317 | } 318 | 319 | /** 320 | * 生成BinaryBitmap 321 | * @param inputStream 输入流 322 | * @return 323 | * @throws IOException 324 | */ 325 | private static BinaryBitmap createBinaryBitmap(InputStream inputStream) throws IOException { 326 | BufferedImage bufferedImage = ImageIO.read(inputStream); 327 | LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); 328 | Binarizer binarizer = new HybridBinarizer(source); 329 | return new BinaryBitmap(binarizer); 330 | } 331 | 332 | /** 333 | * 生成带有文本的条形码 334 | * @param text 生成文本 335 | * @param qrCodeWidth 条形码宽度 336 | * @param qrCodeHeight 条形码高度 337 | * @param format 图片格式 338 | * @param encodeHints 条形码配置 339 | * @return 输出流 340 | * @throws WriterException 341 | * @throws IOException 342 | */ 343 | private static Thumbnails.Builder createWidthLogo(String text, int qrCodeWidth, int qrCodeHeight, String format, 344 | EnumMap encodeHints) throws WriterException, IOException { 345 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.CODE_128, qrCodeWidth, qrCodeHeight, encodeHints); 346 | /* 生成条形码的bufferedImage */ 347 | BufferedImage barcode = MatrixToImageWriter.toBufferedImage(bitMatrix); 348 | /* 创建图片构件对象 */ 349 | BufferedImage bg = new BufferedImage(qrCodeWidth, qrCodeHeight + 10, BufferedImage.TYPE_INT_RGB); 350 | Graphics bgGraphics = bg.getGraphics(); 351 | bgGraphics.setColor(Color.WHITE); 352 | bgGraphics.fillRect(0, 0, qrCodeWidth, qrCodeHeight); 353 | Thumbnails.Builder builder = Thumbnails.of(bg); 354 | String fontFile = BarcodeUtils.class.getClassLoader() 355 | .getResource(BASE_FONT) 356 | .getPath(); 357 | Font font = new Font(fontFile, Font.PLAIN, 12); 358 | BufferedImage textImage = new BufferedImage(qrCodeWidth, font.getSize(), BufferedImage.TYPE_INT_ARGB); 359 | //获得图片的笔刷 360 | Graphics graphics = textImage.getGraphics(); 361 | // 背景颜色 362 | bgGraphics.setColor(Color.WHITE); 363 | graphics.fillRect(0, 0, qrCodeWidth, font.getSize()); 364 | graphics.setColor(Color.BLACK); 365 | //文字居中 366 | FontMetrics fm = graphics.getFontMetrics(); 367 | graphics.setFont(font); 368 | graphics.drawString(text, (qrCodeWidth - fm.stringWidth(text)) / 2 , font.getSize()); 369 | BufferedImage textBufferedImage = Thumbnails.of(textImage).size(qrCodeWidth, font.getSize()).asBufferedImage(); 370 | /* 设置l位置居中,不透明 */ 371 | builder.watermark(Positions.CENTER, barcode, 1F) 372 | .watermark(Positions.BOTTOM_CENTER, textBufferedImage, 1F) 373 | .scale(1F) 374 | .outputFormat(format); 375 | return builder; 376 | } 377 | 378 | } 379 | -------------------------------------------------------------------------------- /src/main/java/org/oriboy/qrcode/QRCodeUtils.java: -------------------------------------------------------------------------------- 1 | package org.oriboy.qrcode; 2 | 3 | import com.google.zxing.*; 4 | import com.google.zxing.client.j2se.BufferedImageLuminanceSource; 5 | import com.google.zxing.client.j2se.MatrixToImageConfig; 6 | import com.google.zxing.client.j2se.MatrixToImageWriter; 7 | import com.google.zxing.common.BitMatrix; 8 | import com.google.zxing.common.HybridBinarizer; 9 | import com.google.zxing.qrcode.QRCodeReader; 10 | import com.google.zxing.qrcode.QRCodeWriter; 11 | import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; 12 | import net.coobird.thumbnailator.Thumbnails; 13 | import net.coobird.thumbnailator.geometry.Positions; 14 | 15 | import javax.imageio.ImageIO; 16 | import java.awt.*; 17 | import java.awt.image.BufferedImage; 18 | import java.io.*; 19 | import java.nio.file.FileSystems; 20 | import java.nio.file.Path; 21 | import java.util.EnumMap; 22 | import java.util.Map; 23 | 24 | /** 25 | * QR code generation parsing class 26 | * 二维码生成/解析类 27 | * @author rocky 28 | * @date 2018-07-17 17:56 29 | */ 30 | public class QRCodeUtils { 31 | 32 | /** 33 | * 二维码默认宽度 34 | */ 35 | private static final int QRCODE_WIDTH = 300; 36 | 37 | /** 38 | * 二维码默认高度 39 | */ 40 | private static final int QRCODE_HEIGHT = 300; 41 | 42 | /** 43 | * logo默认宽度 44 | */ 45 | private static final int LOGO_WIDTH = 90; 46 | 47 | /** 48 | * logo默认高度 49 | */ 50 | private static final int LOGO_HEIGHT = 90; 51 | 52 | /** 53 | * 默认二维码文件格式 54 | */ 55 | private static final String FORMAT = "png"; 56 | 57 | /** 58 | * 生成二维码参数 59 | */ 60 | private static final EnumMap ENCODE_HINTS = new EnumMap<>(EncodeHintType.class); 61 | 62 | /** 63 | * 解析二维码参数 64 | */ 65 | private static final Map DECODE_HINTS = new EnumMap<>(DecodeHintType.class); 66 | 67 | static { 68 | /* 字符编码 */ 69 | ENCODE_HINTS.put(EncodeHintType.CHARACTER_SET, "UTF-8"); 70 | /* 容错等级 L、M、Q、H 其中 L 为最低, H 为最高 */ 71 | ENCODE_HINTS.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); 72 | /* 二维码与图片边距,,默认为0 */ 73 | ENCODE_HINTS.put(EncodeHintType.MARGIN, 0); 74 | 75 | /* 字符编码 */ 76 | DECODE_HINTS.put(DecodeHintType.CHARACTER_SET, "UTF-8"); 77 | } 78 | 79 | private QRCodeUtils() { 80 | 81 | } 82 | 83 | /** 84 | * @title 生成二维码图片 85 | * @description 根据文本内容生成默认格式的二维图图片 86 | * @param text 文本内容 87 | * @param filePath 生成图片路径 88 | * @throws WriterException 89 | * @throws IOException 90 | */ 91 | public static void create(String text, String filePath) throws WriterException, IOException { 92 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, ENCODE_HINTS); 93 | Path path = FileSystems.getDefault().getPath(filePath); 94 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path); 95 | } 96 | 97 | /** 98 | * @title 生成二维码图片 99 | * @description 根据文本内容生成二维码图片的字节输出流 100 | * @param text 文本内容 101 | * @return 字节输出流 102 | * @throws WriterException 103 | * @throws IOException 104 | */ 105 | public static ByteArrayOutputStream create(String text) throws WriterException, IOException { 106 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, QRCODE_WIDTH, QRCODE_HEIGHT, ENCODE_HINTS); 107 | return writeToStream(bitMatrix, FORMAT); 108 | } 109 | 110 | /** 111 | * @title 生成二维码图片 112 | * @description 根据文本内容,自定义宽度高度,生成所需要的二维码图片 113 | * @param text 文本内容 114 | * @param filePath 生成图片文件路径 115 | * @param width 宽度 116 | * @param height 高度 117 | */ 118 | public static void create(String text, String filePath, int width, int height) throws WriterException, IOException { 119 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 120 | Path path = FileSystems.getDefault().getPath(filePath); 121 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path); 122 | } 123 | 124 | /** 125 | * @title 生成二维码图片 126 | * @description 根据文本内容,自定义宽度高度,生成所需要的二维码图片 127 | * @param text 文本内容 128 | * @param filePath 生成图片文件路径 129 | * @param width 宽度 130 | * @param height 高度 131 | * @param onColor 二维码颜色 132 | * @param offColor 二维码背景颜色 133 | */ 134 | public static void create(String text, String filePath, int width, int height, Color onColor, Color offColor) throws WriterException, IOException { 135 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 136 | Path path = FileSystems.getDefault().getPath(filePath); 137 | MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, path, new MatrixToImageConfig(onColor.getRGB(), offColor.getRGB())); 138 | } 139 | 140 | /** 141 | * @title 生成二维码图片 142 | * @description 根据文本内容生成二维码图片的字节输出流 143 | * @param text 文本内容 144 | * @param width 宽度 145 | * @param height 高度 146 | * @return 字节输出流 147 | */ 148 | public static ByteArrayOutputStream create(String text, int width, int height) throws WriterException, IOException { 149 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 150 | return writeToStream(bitMatrix, FORMAT); 151 | } 152 | 153 | /** 154 | * @title 生成二维码图片 155 | * @description 根据文本内容,自定义宽度高度,自定义图片格式,生成所需要的二维码图片 156 | * @param text 文本内容 157 | * @param filePath 生成图片文件路径 158 | * @param width 宽度 159 | * @param height 高度 160 | * @param format 图片格式 161 | */ 162 | public static void create(String text, String filePath, int width, int height, String format) throws WriterException, IOException { 163 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 164 | Path path = FileSystems.getDefault().getPath(filePath); 165 | MatrixToImageWriter.writeToPath(bitMatrix, format, path); 166 | } 167 | 168 | /** 169 | * @title 生成二维码图片 170 | * @description 根据文本内容生成二维码图片的字节输出流 171 | * @param text 文本内容 172 | * @param width 宽度 173 | * @param height 高度 174 | * @param format 自定义图片格式 175 | * @return 字节输出流 176 | */ 177 | public static ByteArrayOutputStream create(String text, int width, int height, String format) throws WriterException, IOException { 178 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, ENCODE_HINTS); 179 | return writeToStream(bitMatrix, format); 180 | } 181 | 182 | /** 183 | * @title 生成二维码图片 184 | * @description 根据文本内容,自定义宽度高度,自定义图片格式,自定义配置信息,生成所需要的二维码图片 185 | * @param text 文本内容 186 | * @param filePath 生成图片文件路径 187 | * @param width 宽度 188 | * @param height 高度 189 | * @param format 图片格式 190 | * @param hintTypes 配置信息 191 | */ 192 | public static void create(String text, String filePath, int width, int height, String format, Map hintTypes) throws WriterException, IOException { 193 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes); 194 | Path path = FileSystems.getDefault().getPath(filePath); 195 | MatrixToImageWriter.writeToPath(bitMatrix, format, path); 196 | } 197 | 198 | /** 199 | * @title 生成二维码图片 200 | * @param text 文本内容 201 | * @param width 宽度 202 | * @param height 高度 203 | * @param format 图片格式 204 | * @param hintTypes 配置信息 205 | * @return 字节输出流 206 | * @throws WriterException 207 | * @throws IOException 208 | */ 209 | public static ByteArrayOutputStream create(String text, int width, int height, String format, Map hintTypes) throws WriterException, IOException { 210 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, width, height, hintTypes); 211 | return writeToStream(bitMatrix, format); 212 | } 213 | 214 | 215 | /** 216 | * @title 解析二维码 217 | * @description 解析二维码,获取其中文本信息 218 | * @param filePath 图片路径 219 | * @return 文本信息 220 | * @throws IOException 221 | * @throws FormatException 222 | * @throws ChecksumException 223 | * @throws NotFoundException 224 | */ 225 | public static String read(String filePath) throws IOException, FormatException, ChecksumException, NotFoundException { 226 | BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath)); 227 | return readQRcode(binaryBitmap, DECODE_HINTS).getText(); 228 | } 229 | 230 | /** 231 | * @title 解析二维码 232 | * @description 解析二维码,获取其中文本信息 233 | * @param inputStream 输入流 234 | * @return 文本信息 235 | * @throws IOException 236 | * @throws FormatException 237 | * @throws ChecksumException 238 | * @throws NotFoundException 239 | */ 240 | public static String read(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException { 241 | BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream); 242 | return readQRcode(binaryBitmap, DECODE_HINTS).getText(); 243 | } 244 | 245 | /** 246 | * @title 解析二维码 247 | * @description 解析二维码,获取其中文本信息 248 | * @param filePath 图片路径 249 | * @param decodeHints 解析配置 250 | * @return 文本信息 251 | * @throws FormatException 252 | * @throws ChecksumException 253 | * @throws NotFoundException 254 | * @throws IOException 255 | */ 256 | private static String read(String filePath, Map decodeHints) throws FormatException, ChecksumException, NotFoundException, IOException { 257 | BinaryBitmap binaryBitmap = createBinaryBitmap(new FileInputStream(filePath)); 258 | return readQRcode(binaryBitmap, decodeHints).getText(); 259 | } 260 | 261 | /** 262 | * @title 解析二维码 263 | * @description 解析二维码,获取其中文本信息 264 | * @param inputStream 输入流 265 | * @return 文本信息 266 | * @throws IOException 267 | * @throws FormatException 268 | * @throws ChecksumException 269 | * @throws NotFoundException 270 | */ 271 | public static String read(InputStream inputStream, Map decodeHints) throws IOException, FormatException, ChecksumException, NotFoundException { 272 | BinaryBitmap binaryBitmap = createBinaryBitmap(inputStream); 273 | return readQRcode(binaryBitmap, decodeHints).getText(); 274 | } 275 | 276 | /** 277 | * 生成带logo的二维码图片 278 | * @param text 文本内容 279 | * @param filePath 生成文件存放路径 280 | * @param logoPath logo文件路径 281 | * @throws WriterException 282 | * @throws IOException 283 | */ 284 | public static void createWithLogo(String text, String filePath, String logoPath) throws WriterException, IOException { 285 | createWidthLogo(text, new FileInputStream(logoPath), QRCODE_WIDTH, QRCODE_HEIGHT, 286 | LOGO_WIDTH, LOGO_HEIGHT, FORMAT, ENCODE_HINTS) 287 | .toFile(filePath); 288 | } 289 | 290 | /** 291 | * 生成带logo的二维码图片 292 | * @param text 文本内容 293 | * @param filePath 生成文件存放路径 294 | * @param logoPath logo文件路径 295 | * @param qrCodeWidth 二维码宽度 296 | * @param qrCodeHeight 二维码高度 297 | * @param logoWidth logo宽度 298 | * @param logoHeight logo高度 299 | * @throws IOException 300 | * @throws WriterException 301 | */ 302 | public static void createWithLogo(String text, String filePath, String logoPath, int qrCodeWidth, int qrCodeHeight, 303 | int logoWidth, int logoHeight) throws IOException, WriterException { 304 | createWidthLogo(text, new FileInputStream(logoPath), qrCodeWidth, qrCodeHeight, 305 | logoWidth, logoHeight, FORMAT, ENCODE_HINTS) 306 | .toFile(filePath); 307 | } 308 | 309 | /** 310 | * 生成带logo的二维码图片 311 | * @param text 文本内容 312 | * @param filePath 生成文件存放路径 313 | * @param logoPath logo文件路径 314 | * @param qrCodeWidth 二维码宽度 315 | * @param qrCodeHeight 二维码高度 316 | * @param logoWidth logo宽度 317 | * @param logoHeight logo高度 318 | * @param format 图片格式 319 | * @throws IOException 320 | * @throws WriterException 321 | */ 322 | public static void createWithLogo(String text, String filePath, String logoPath, int qrCodeWidth, int qrCodeHeight, 323 | int logoWidth, int logoHeight, String format) throws IOException, WriterException { 324 | createWidthLogo(text, new FileInputStream(logoPath), qrCodeWidth, qrCodeHeight, 325 | logoWidth, logoHeight, format, ENCODE_HINTS) 326 | .toFile(filePath); 327 | } 328 | 329 | /** 330 | * 生成带logo的二维码图片 331 | * @param text 文本内容 332 | * @param filePath 生成文件存放路径 333 | * @param logoPath logo文件路径 334 | * @param hintTypes 生成配置 335 | */ 336 | public static void createWithLogo(String text, String filePath, String logoPath, Map hintTypes) throws IOException, WriterException { 337 | createWidthLogo(text, new FileInputStream(logoPath), QRCODE_WIDTH, QRCODE_WIDTH, 338 | LOGO_WIDTH, LOGO_WIDTH, FORMAT, hintTypes) 339 | .toFile(filePath); 340 | } 341 | 342 | 343 | /** 344 | * 生成带有logo的二维码图片 345 | * @param text 文本内容 346 | * @param logoPath logo存放路径 347 | * @return 生成的输出流 348 | * @throws IOException 349 | * @throws WriterException 350 | */ 351 | public static OutputStream createWithLogo(String text, String logoPath) throws IOException, WriterException { 352 | OutputStream outputStream = new ByteArrayOutputStream(); 353 | createWidthLogo(text, new FileInputStream(logoPath), QRCODE_WIDTH, QRCODE_HEIGHT, 354 | LOGO_WIDTH, LOGO_HEIGHT, FORMAT, ENCODE_HINTS) 355 | .toOutputStream(outputStream); 356 | return outputStream; 357 | } 358 | 359 | 360 | /** 361 | * 生成带有logo的二维码图片 362 | * @param text 文本内容 363 | * @param logoPath logo存放路径 364 | * @param qrCodeWidth 二维码宽度 365 | * @param qrCodeHeight 二维码高度 366 | * @param logoWidth logo宽度 367 | * @param logoHeight logo高度 368 | * @return 生成的输出流 369 | */ 370 | public static OutputStream createWithLogo(String text, String logoPath, int qrCodeWidth, int qrCodeHeight, int logoWidth, int logoHeight) 371 | throws IOException, WriterException { 372 | OutputStream outputStream = new ByteArrayOutputStream(); 373 | createWidthLogo(text, new FileInputStream(logoPath), qrCodeWidth, qrCodeHeight, 374 | logoWidth, logoHeight, FORMAT, ENCODE_HINTS) 375 | .toOutputStream(outputStream); 376 | return outputStream; 377 | } 378 | 379 | /** 380 | * 生成带有logo的二维码图片 381 | * @param text 文本内容 382 | * @param logoPath logo存放路径 383 | * @param qrCodeWidth 二维码宽度 384 | * @param qrCodeHeight 二维码高度 385 | * @param logoWidth logo宽度 386 | * @param logoHeight logo高度 387 | * @param format 图片格式 388 | * @return 生成的输出流 389 | */ 390 | public static OutputStream createWithLogo(String text, String logoPath, int qrCodeWidth, int qrCodeHeight, int logoWidth, 391 | int logoHeight, String format) throws IOException, WriterException { 392 | OutputStream outputStream = new ByteArrayOutputStream(); 393 | createWidthLogo(text, new FileInputStream(logoPath), qrCodeWidth, qrCodeHeight, 394 | logoWidth, logoHeight, format, ENCODE_HINTS) 395 | .toOutputStream(outputStream); 396 | return outputStream; 397 | } 398 | 399 | 400 | /** 401 | * @title 生成BitMatrix 402 | * @param text 文本内容 403 | * @param barcodeFormat 二维码格式 404 | * @param width 宽度 405 | * @param height 高度 406 | * @param hintTypes 配置 407 | * @return BitMatrix 408 | * @throws WriterException 409 | */ 410 | private static BitMatrix createBitMatrix(String text, BarcodeFormat barcodeFormat, int width, int height, Map hintTypes) 411 | throws WriterException { 412 | QRCodeWriter qrCodeWriter = new QRCodeWriter(); 413 | return qrCodeWriter.encode(text, barcodeFormat, width, height, hintTypes); 414 | } 415 | 416 | /** 417 | * 转成字符输出流 418 | * @param bitMatrix bitMatrix 419 | * @param format 图片格式 420 | * @return 421 | * @throws IOException 422 | */ 423 | private static ByteArrayOutputStream writeToStream(BitMatrix bitMatrix, String format) throws IOException { 424 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 425 | MatrixToImageWriter.writeToStream(bitMatrix, format, outputStream); 426 | return outputStream; 427 | } 428 | 429 | /** 430 | * 解析二维码 431 | * @param binaryBitmap 二维码图类 432 | * @return 解析信息 433 | * @throws FormatException 434 | * @throws ChecksumException 435 | * @throws NotFoundException 436 | */ 437 | private static Result readQRcode(BinaryBitmap binaryBitmap, Map decodeHints) throws FormatException, ChecksumException, NotFoundException { 438 | QRCodeReader reader = new QRCodeReader(); 439 | return reader.decode(binaryBitmap, decodeHints); 440 | } 441 | 442 | /** 443 | * 生成BinaryBitmap 444 | * @param inputStream 输入流 445 | * @return 446 | * @throws IOException 447 | */ 448 | private static BinaryBitmap createBinaryBitmap(InputStream inputStream) throws IOException { 449 | BufferedImage bufferedImage = ImageIO.read(inputStream); 450 | LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage); 451 | Binarizer binarizer = new HybridBinarizer(source); 452 | return new BinaryBitmap(binarizer); 453 | } 454 | 455 | /** 456 | * 生成带有logo的二维码 457 | * @param text 生成文本 458 | * @param logoInputStream logo文件输入流 459 | * @param qrCodeWidth 二维码宽度 460 | * @param qrCodeHeight 二维码高度 461 | * @param logoWidth logo宽度 462 | * @param logoHeight logo高度 463 | * @param encodeHints 二维码配置 464 | * @return 输出流 465 | * @throws WriterException 466 | * @throws IOException 467 | */ 468 | private static Thumbnails.Builder createWidthLogo(String text, InputStream logoInputStream, int qrCodeWidth, int qrCodeHeight, int logoWidth, int logoHeight, 469 | String format, Map encodeHints) throws WriterException, IOException { 470 | BitMatrix bitMatrix = createBitMatrix(text, BarcodeFormat.QR_CODE, qrCodeWidth, qrCodeHeight, encodeHints); 471 | /* 生成二维码的bufferedImage */ 472 | BufferedImage qrcode = MatrixToImageWriter.toBufferedImage(bitMatrix); 473 | /* 创建图片构件对象 */ 474 | Thumbnails.Builder builder = Thumbnails.of(new BufferedImage(qrCodeWidth, qrCodeHeight, BufferedImage.TYPE_INT_RGB)); 475 | BufferedImage logo = ImageIO.read(logoInputStream); 476 | BufferedImage logoImage = Thumbnails.of(logo).size(logoWidth, logoHeight).asBufferedImage(); 477 | /* 设置logo水印位置居中,不透明 */ 478 | builder.watermark(Positions.CENTER, qrcode, 1F) 479 | .watermark(Positions.CENTER, logoImage, 1F) 480 | .scale(1F) 481 | .outputFormat(format); 482 | return builder; 483 | } 484 | 485 | } 486 | -------------------------------------------------------------------------------- /src/main/resources/font/msyh.ttc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ToView/qrcodecore/23a868a392b833ed9b5a0b3313139c27b6a84a2a/src/main/resources/font/msyh.ttc --------------------------------------------------------------------------------