好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java生成读取条形码和二维码的简单示例

条形码

将宽度不等的多个黑条和白条,按照一定的编码规则排序,用以表达一组信息的图像标识符

通常代表一串数字 / 字母,每一位有特殊含义

一般数据容量30个数字 / 字母

二维码

用某种特定几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息

比一维条形码能存储更多信息,表示更多数据类型

能够存储数字 / 字母 / 汉字 / 图片等信息

可存储几百到几十kb字符

zxing

zxing主要是google出品的,用于识别一维码和二维码的第三方库

主要类:

bitmatrix位图矩阵 multiformatwriter位图编写器 matrixtoimagewriter写入图片

maven导入zxing

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<dependencies>

         <!-- https: //mvnrepository测试数据/artifact/com.google.zxing/javase -->

         <dependency>

             <groupid>com.google.zxing</groupid>

             <artifactid>javase</artifactid>

             <version> 3.2 . 1 </version>

         </dependency>

 

         <dependency>

             <groupid>com.google.zxing</groupid>

             <artifactid>core</artifactid>

             <version> 3.0 . 0 </version>

         </dependency>

</dependencies>

生成一维码java

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

public static void main(string[] args) {

     generatecode( new file( "1dcode.png" ), "1390351289" , 500 , 250 );

}

/**

  * @param file    生成的文件名称

  * @param code    一维码存储的数据信息

  * @param width   生成图片的宽度

  * @param height  生成图片的高度

  * @return void

  * */

public static void generatecode(file file, string code, int width, int height){

     // 定义位图矩阵bitmatrix

     bitmatrix matrix = null ;

     try {

         // 使用code_128格式进行编码生成100*25的条形码

         multiformatwriter writer = new multiformatwriter();

 

         matrix = writer.encode(code, barcodeformat.code_128, width, height, null );

     } catch (writerexception e) {

         e.printstacktrace();

     }

 

     // 将位图矩阵bitmatrix保存为图片

     try {

         fileoutputstream outputstream = new fileoutputstream(file);

         imageio.write(matrixtoimagewriter.tobufferedimage(matrix), "png" , outputstream);

         outputstream.flush();

         outputstream.close();

     } catch (exception e) {

         e.printstacktrace();

     }

}

注意: 一维码只能存储数字和字母,其他数据会报failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project mavendemo: command execution failed.错误java

读取一维码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

public static void main(string[] args) {

     readcode( new file( "1dcode.png" ));

}

/**

  * @param readimage    读取一维码图片名

  * @return void

  * */

public static void readcode(file readimage) {

     try {

         bufferedimage image = imageio.read(readimage);

         if (image == null ) {

             return ;

         }

         luminancesource source = new bufferedimageluminancesource(image);

         binarybitmap bitmap = new binarybitmap( new hybridbinarizer(source));

 

         map<decodehinttype, object> hints = new hashmap<decodehinttype, object>();

         hints.put(decodehinttype.character_set, "gbk" );

         hints.put(decodehinttype.pure_barcode, boolean . true );

         hints.put(decodehinttype.try_harder, boolean . true );

 

         result result = new multiformatreader().decode(bitmap, hints);

         system.out.println(result.gettext());

     } catch (exception e) {

         e.printstacktrace();

     }

}

注意:当使用string类进行转码时,要使用java.lang包的,maven导包的时候会导入第三方apache的string类

生成二维码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

/** 定义二维码的宽度 */

private final static int width = 300 ;

/** 定义二维码的高度 */

private final static int height = 300 ;

/** 定义二维码的格式 */

private final static string format = "png" ;

 

/**

  * @param file

  * @param content

  * @return void

  * */

public static void generateqrcode(file file, string content) {

     // 定义二维码参数

     map<encodehinttype, object> hints = new hashmap<encodehinttype, object>();

     // 设置编码

     hints.put(encodehinttype.character_set, "utf-8" );

     // 设置容错等级

     hints.put(encodehinttype.error_correction, errorcorrectionlevel.m);

     // 设置边距,默认为5

     hints.put(encodehinttype.margin, 2 );

 

     try {

         bitmatrix bitmatrix = new multiformatwriter()

                 .encode(content, barcodeformat.qr_code, width, height, hints);

         path path = file.topath();

         // 保存到项目跟目录中

         matrixtoimagewriter.writetopath(bitmatrix, format, path);

     } catch (exception e) {

         e.printstacktrace();

     }

}

public static void main(string[] args) {

     generateqrcode( new file( "smt.png" ), "淑玫唐家居网" );

}

读取二维码

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

  * @param file    读取二维码的文件名

  * @return void

  * */

public static void readqrcode(file file) {

     multiformatreader reader = new multiformatreader();

     try {

         bufferedimage image = imageio.read(file);

         binarybitmap binarybitmap = new binarybitmap( new hybridbinarizer( new bufferedimageluminancesource(image)));

         map<decodehinttype, object> hints = new hashmap<>();

         hints.put(decodehinttype.character_set, "utf-8" );

         result result = reader.decode(binarybitmap, hints);

         system.out.println( "解析结果: " + new string(result.tostring().getbytes( "gbk" ), "gbk" ));

         system.out.println( "二维码格式: " + result.getbarcodeformat());

         system.out.println( "二维码文本内容: " + new string(result.gettext().getbytes( "gbk" ), "gbk" ));

     } catch (exception e) {

         e.printstacktrace();

     }

}

public static void main(string[] args) {

     readqrcode( new file( "smt.png" ));

}

注意: maven打印的控制台中会出现中文乱码,在idea setting->maven->runner vmoptions:-dfile.encoding=gb2312;即可解决

总结

到此这篇关于java生成读取条形码和二维码的文章就介绍到这了,更多相关java生成读取条形码二维码内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/weixin_43730516/article/details/117927316

查看更多关于Java生成读取条形码和二维码的简单示例的详细内容...

  阅读:29次