• 条形码(JBarcode)


    商品条形码(JBarcode)

    之前没有使用过这个,现在使用JBarcode生成商品条形码,工作之前的准备工作:

    Eclipse
    Eclipse Java EE IDE for Web Developers.
    Version: Helios Service Release 1
    Build id: 20100917-0705

    jar包
    JBarcode-Recognition_Source-0.2.jar
    jbarcode-0.2.8.jar
    commons-lang-2.6.jar

    首先了解EAN-13码的规则:

    然后大家去了解一下这些数字的排列:

    13位条形码分位处理就看出来,这些都需要自己加工处理并做截取处理,可以了解条形码每个段位表达的意思。

    知道这些就已经足够我们去做一个条形码的校验工作以及生成自己的条形码。

    了解校验码是怎么回事,我们根据我们自己的需求去做,然后根据需求处理一下,就是我们想要的条形码。

    校验码生成规则如下:

    注意:这里的校验码,如果减掉后的C的结果为0或者10,那么最后一位的校验码就是0

    现在是不是对JBarcode越来越感兴趣了呢,流程是很简单的。

    明天小媳妇的巧克力就到了,加油写代码为了小媳妇的巧克力。,,,

     

    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
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    package com.liuyc.test.demo;
     
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.UnsupportedEncodingException;
    import java.net.URLDecoder;
    import java.util.regex.Pattern;
     
    import org.apache.commons.lang.StringUtils;
    import org.jbarcode.JBarcode;
    import org.jbarcode.encode.EAN13Encoder;
    import org.jbarcode.paint.EAN13TextPainter;
    import org.jbarcode.paint.WideRatioCodedPainter;
    import org.jbarcode.paint.WidthCodedPainter;
    import org.jbarcode.util.ImageUtil;
     
    /**
     *
     * @=============================================
     *
     * @author : Liuyc
     * @create : 2015年1月26日 14:47:57
     * @update :
     * @E-mail : 763999883@qq.com
     * @desc :
     *
     * @=============================================
     */
    public class BarCodeImage {
     
        /**
         * 图片类型
         */
        public enum ImgType {
            /**
             * 图片格式:.gif
             */
            GIF(".gif"),
            /**
             * 图片格式:.png
             */
            PNG(".png"),
            /**
             * 图片格式:.jpg
             */
            JPG(".jpg"),
            /**
             * 图片格式:.jpeg
             */
            JPEG(".jpeg"), ;
     
            ImgType(String value) {
                this.value = value;
            }
     
            private final String value;
     
            public String getValue() {
                return value;
            }
        }
     
        /**
         * 生成商品条形码
         *
         * @param filePath
         *            商品条形码图片存放路径:../xxx/yyy/
         * @param jbarCode
         *            商品条形码:8位、13位
         * @param format
         *            商品条形码图片格式:.gif/.png/.jpg/.jpeg
         * @return 图片存放路径+图片名称+图片文件类型
         */
        public String createBarCode(String filePath, String jbarCode, String format) {
            String barCodeName = jbarCode + format;
            try {
                BufferedImage bi = null;
                int len = jbarCode.length();
                String barCode = jbarCode;
                if (len == 12) {
     
                else if (len == 13) {
                    int backCode = checkCode(jbarCode);
                    int oldCode = Integer
                            .parseInt(jbarCode.substring(len - 1, len));
                    if (oldCode != backCode) {
                        return null;
                    }
                    barCode = jbarCode.substring(0, jbarCode.length() - 1);
                }
                 
     
                JBarcode localJBarcode13 = new JBarcode(EAN13Encoder.getInstance(),
                        WidthCodedPainter.getInstance(),
                        EAN13TextPainter.getInstance());
     
                bi = localJBarcode13.createBarcode(barCode);
     
                if (ImgType.GIF.equals(format)) {
                    saveToGIF(bi, filePath, barCodeName);
                else if (ImgType.PNG.equals(format)) {
                    saveToPNG(bi, filePath, barCodeName);
                else if (ImgType.JPG.equals(format) || ImgType.JPEG.equals(format)) {
                    saveToJPEG(bi, filePath, barCodeName);
                }
     
                localJBarcode13.setEncoder(EAN13Encoder.getInstance());
                localJBarcode13.setPainter(WideRatioCodedPainter.getInstance());
                localJBarcode13.setTextPainter(EAN13TextPainter.getInstance());
                localJBarcode13.setShowCheckDigit(false);
                return filePath + barCodeName;
            catch (Exception localException) {
                localException.printStackTrace();
                return null;
            }
        }
     
        /**
         * 生成JPEG图片
         *
         * @param paramBufferedImage
         * @param paramString
         */
        @SuppressWarnings("unused")
        private void saveToJPEG(BufferedImage paramBufferedImage, String filePath,
                String fileName) {
            saveToFile(paramBufferedImage, filePath, fileName, "jpeg");
        }
     
        /**
         * 生成PNG图片
         *
         * @param paramBufferedImage
         * @param paramString
         */
        @SuppressWarnings("unused")
        private void saveToPNG(BufferedImage paramBufferedImage, String filePath,
                String fileName) {
            saveToFile(paramBufferedImage, filePath, fileName, "png");
        }
     
        /**
         * 生成GIF图片
         *
         * @param paramBufferedImage
         * @param paramString
         */
        private void saveToGIF(BufferedImage paramBufferedImage, String filePath,
                String fileName) {
            saveToFile(paramBufferedImage, filePath, fileName, "gif");
        }
     
        /**
         * 保存图片文件
         *
         * @param paramBufferedImage
         *            图片流
         * @param filePath
         *            文件路径
         * @param imgName
         *            图片参数
         * @param imgFormat
         *            图片格式
         */
        private void saveToFile(BufferedImage paramBufferedImage, String filePath,
                String imgName, String imgFormat) {
            try {
                FileOutputStream fileOutputStream = null;
                try {
                    String rootPath = this.getClass().getClassLoader()
                            .getResource("/").getPath();
                    String imgDir = StringUtils
                            .substringBefore(rootPath, "WEB-INF").concat(filePath);
                    String dirPath = "";
                    try {
                        dirPath = URLDecoder.decode(imgDir, "UTF-8");
                    catch (UnsupportedEncodingException uee) {
                        uee.printStackTrace();
                    }
                    File dirFile = new File(dirPath);
                    if (!dirFile.exists()) {
                        dirFile.mkdirs();
                    }
                    String imgPath = dirPath + "/" + imgName;
                    fileOutputStream = new FileOutputStream(imgPath);
                catch (Exception e) {
                    System.out.println("Create Img File Error:" + e.toString());
                }
                ImageUtil.encodeAndWrite(paramBufferedImage, imgFormat,
                        fileOutputStream, 9696);
                fileOutputStream.close();
            catch (Exception localException) {
                System.out.println("Save Img File Error:" + localException);
                localException.printStackTrace();
            }
        }
     
        /**
         * 返回校验码
         *
         * @param code
         *            商品条形码
         * @return 校验码: -1:格式不正确,条形码为全部数字 -2:参数不能为空
         *
         */
        private int checkCode(String code) {
            int checkCode = -1;
            if (code == null || "".equals(code)) {
                return -2;
            else if (!Pattern.compile("^[0-9]*$").matcher(code).matches()) {
                checkCode = -1;
            else {
                try {
                    int evensum = 0// 偶数位的和
                    int oddsum = 0// 奇数位的和
                    evensum += Integer.parseInt(code.substring(1112));
                    evensum += Integer.parseInt(code.substring(910));
                    evensum += Integer.parseInt(code.substring(78));
                    evensum += Integer.parseInt(code.substring(56));
                    evensum += Integer.parseInt(code.substring(34));
                    evensum += Integer.parseInt(code.substring(12));
                    evensum *= 3;
                    oddsum += Integer.parseInt(code.substring(1011));
                    oddsum += Integer.parseInt(code.substring(89));
                    oddsum += Integer.parseInt(code.substring(67));
                    oddsum += Integer.parseInt(code.substring(45));
                    oddsum += Integer.parseInt(code.substring(23));
                    oddsum += Integer.parseInt(code.substring(01));
                    int sum = evensum + oddsum;
                    int ck = 0;
                    if (sum % 10 == 0) {
                        ck = sum;
                    else {
                        ck = (sum / 10 1) * 10;
                    }
                    checkCode = ck - sum;
                catch (NumberFormatException e) {
                    System.out.println("BarCode Format Error:" + e.toString());
                catch (Exception e) {
                    System.out.println("Get Check Code Error:" + e.toString());
                }
            }
            return checkCode;
        }
     
        /**
         * @param args
         */
        public static void main(String[] args) {
     
             
     
        }
     
    }

    载请标明出处,出处地址 http://www.cnblogs.com/yuchuan/p/4250328.html
     
    分类: Java
  • 相关阅读:
    机器学习知识体系
    Request
    Http协议
    Servlet 学习
    Tomcat 服务器
    XML文件
    StringJdbc :jdbcTemplate
    Druid 数据库连接池
    c3p0配置文件(c3p0.properties.xml)解读
    数据库连接池 C3p0
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4252591.html
Copyright © 2020-2023  润新知