⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 barcodeean.java

📁 一个java操作pdf文件的开发包,很好用的.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        boolean flip = (code[0] != 0);        int pb = 0;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 1;        byte sequence[] = PARITYE[code[code.length - 1]];        for (int k = 1; k < code.length - 1; ++k) {            int c = code[k];            byte stripes[] = BARS[c];            if (sequence[k - 1] == (flip ? EVEN : ODD)) {                bars[pb++] = stripes[0];                bars[pb++] = stripes[1];                bars[pb++] = stripes[2];                bars[pb++] = stripes[3];            }            else {                bars[pb++] = stripes[3];                bars[pb++] = stripes[2];                bars[pb++] = stripes[1];                bars[pb++] = stripes[0];            }        }        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 1;        return bars;    }    /** Creates the bars for the barcode supplemental 2.     * @param _code the text with 2 digits     * @return the barcode     */        public static byte[] getBarsSupplemental2(String _code) {        int code[] = new int[2];        for (int k = 0; k < code.length; ++k)            code[k] = _code.charAt(k) - '0';        byte bars[] = new byte[TOTALBARS_SUPP2];        int pb = 0;        int parity = (code[0] * 10 + code[1]) % 4;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 2;        byte sequence[] = PARITY2[parity];        for (int k = 0; k < sequence.length; ++k) {            if (k == 1) {                bars[pb++] = 1;                bars[pb++] = 1;            }            int c = code[k];            byte stripes[] = BARS[c];            if (sequence[k] == ODD) {                bars[pb++] = stripes[0];                bars[pb++] = stripes[1];                bars[pb++] = stripes[2];                bars[pb++] = stripes[3];            }            else {                bars[pb++] = stripes[3];                bars[pb++] = stripes[2];                bars[pb++] = stripes[1];                bars[pb++] = stripes[0];            }        }        return bars;    }       /** Creates the bars for the barcode supplemental 5.     * @param _code the text with 5 digits     * @return the barcode     */        public static byte[] getBarsSupplemental5(String _code) {        int code[] = new int[5];        for (int k = 0; k < code.length; ++k)            code[k] = _code.charAt(k) - '0';        byte bars[] = new byte[TOTALBARS_SUPP5];        int pb = 0;        int parity = (((code[0] + code[2] + code[4]) * 3) + ((code[1] + code[3]) * 9)) % 10;        bars[pb++] = 1;        bars[pb++] = 1;        bars[pb++] = 2;        byte sequence[] = PARITY5[parity];        for (int k = 0; k < sequence.length; ++k) {            if (k != 0) {                bars[pb++] = 1;                bars[pb++] = 1;            }            int c = code[k];            byte stripes[] = BARS[c];            if (sequence[k] == ODD) {                bars[pb++] = stripes[0];                bars[pb++] = stripes[1];                bars[pb++] = stripes[2];                bars[pb++] = stripes[3];            }            else {                bars[pb++] = stripes[3];                bars[pb++] = stripes[2];                bars[pb++] = stripes[1];                bars[pb++] = stripes[0];            }        }        return bars;    }           /** Gets the maximum area that the barcode and the text, if     * any, will occupy. The lower left corner is always (0, 0).     * @return the size the barcode occupies.     */        public Rectangle getBarcodeSize() {        float width = 0;        float height = barHeight;        if (font != null) {            if (baseline <= 0)                height += -baseline + size;            else                height += baseline - font.getFontDescriptor(BaseFont.DESCENT, size);        }        switch (codeType) {            case EAN13:                width = x * (11 + 12 * 7);                if (font != null) {                    width += font.getWidthPoint(code.charAt(0), size);                }                break;            case EAN8:                width = x * (11 + 8 * 7);                break;            case UPCA:                width = x * (11 + 12 * 7);                if (font != null) {                    width += font.getWidthPoint(code.charAt(0), size) + font.getWidthPoint(code.charAt(11), size);                }                break;            case UPCE:                width = x * (9 + 6 * 7);                if (font != null) {                    width += font.getWidthPoint(code.charAt(0), size) + font.getWidthPoint(code.charAt(7), size);                }                break;            case SUPP2:                width = x * (6 + 2 * 7);                break;            case SUPP5:                width = x * (4 + 5 * 7 + 4 * 2);                break;            default:                throw new RuntimeException("Invalid code type.");        }        return new Rectangle(width, height);    }        /** Places the barcode in a <CODE>PdfContentByte</CODE>. The     * barcode is always placed at coodinates (0, 0). Use the     * translation matrix to move it elsewhere.<p>     * The bars and text are written in the following colors:<p>     * <P><TABLE BORDER=1>     * <TR>     *    <TH><P><CODE>barColor</CODE></TH>     *    <TH><P><CODE>textColor</CODE></TH>     *    <TH><P>Result</TH>     *    </TR>     * <TR>     *    <TD><P><CODE>null</CODE></TD>     *    <TD><P><CODE>null</CODE></TD>     *    <TD><P>bars and text painted with current fill color</TD>     *    </TR>     * <TR>     *    <TD><P><CODE>barColor</CODE></TD>     *    <TD><P><CODE>null</CODE></TD>     *    <TD><P>bars and text painted with <CODE>barColor</CODE></TD>     *    </TR>     * <TR>     *    <TD><P><CODE>null</CODE></TD>     *    <TD><P><CODE>textColor</CODE></TD>     *    <TD><P>bars painted with current color<br>text painted with <CODE>textColor</CODE></TD>     *    </TR>     * <TR>     *    <TD><P><CODE>barColor</CODE></TD>     *    <TD><P><CODE>textColor</CODE></TD>     *    <TD><P>bars painted with <CODE>barColor</CODE><br>text painted with <CODE>textColor</CODE></TD>     *    </TR>     * </TABLE>     * @param cb the <CODE>PdfContentByte</CODE> where the barcode will be placed     * @param barColor the color of the bars. It can be <CODE>null</CODE>     * @param textColor the color of the text. It can be <CODE>null</CODE>     * @return the dimensions the barcode occupies     */        public Rectangle placeBarcode(PdfContentByte cb, Color barColor, Color textColor) {        Rectangle rect = getBarcodeSize();        float barStartX = 0;        float barStartY = 0;        float textStartY = 0;        if (font != null) {            if (baseline <= 0)                textStartY = barHeight - baseline;            else {                textStartY = -font.getFontDescriptor(BaseFont.DESCENT, size);                barStartY = textStartY + baseline;            }        }        switch (codeType) {            case EAN13:            case UPCA:            case UPCE:                if (font != null)                    barStartX += font.getWidthPoint(code.charAt(0), size);                break;        }        byte bars[] = null;        int guard[] = GUARD_EMPTY;        switch (codeType) {            case EAN13:                bars = getBarsEAN13(code);                guard = GUARD_EAN13;                break;            case EAN8:                bars = getBarsEAN8(code);                guard = GUARD_EAN8;                break;            case UPCA:                bars = getBarsEAN13("0" + code);                guard = GUARD_UPCA;                break;            case UPCE:                bars = getBarsUPCE(code);                guard = GUARD_UPCE;                break;            case SUPP2:                bars = getBarsSupplemental2(code);                break;            case SUPP5:                bars = getBarsSupplemental5(code);                break;        }        float keepBarX = barStartX;        boolean print = true;        float gd = 0;        if (font != null && baseline > 0 && guardBars) {            gd = baseline / 2;        }        if (barColor != null)            cb.setColorFill(barColor);        for (int k = 0; k < bars.length; ++k) {            float w = bars[k] * x;            if (print) {                if (Arrays.binarySearch(guard, k) >= 0)                    cb.rectangle(barStartX, barStartY - gd, w, barHeight + gd);                else                    cb.rectangle(barStartX, barStartY, w, barHeight);            }            print = !print;            barStartX += w;        }        cb.fill();        if (font != null) {            if (textColor != null)                cb.setColorFill(textColor);            cb.beginText();            cb.setFontAndSize(font, size);            switch (codeType) {                case EAN13:                    cb.setTextMatrix(0, textStartY);                    cb.showText(code.substring(0, 1));                    for (int k = 1; k < 13; ++k) {                        String c = code.substring(k, k + 1);                        float len = font.getWidthPoint(c, size);                        float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2;                        cb.setTextMatrix(pX, textStartY);                        cb.showText(c);                    }                    break;                case EAN8:                    for (int k = 0; k < 8; ++k) {                        String c = code.substring(k, k + 1);                        float len = font.getWidthPoint(c, size);                        float pX = TEXTPOS_EAN8[k] * x - len / 2;                        cb.setTextMatrix(pX, textStartY);                        cb.showText(c);                    }                    break;                case UPCA:                    cb.setTextMatrix(0, textStartY);                    cb.showText(code.substring(0, 1));                    for (int k = 1; k < 11; ++k) {                        String c = code.substring(k, k + 1);                        float len = font.getWidthPoint(c, size);                        float pX = keepBarX + TEXTPOS_EAN13[k] * x - len / 2;                        cb.setTextMatrix(pX, textStartY);                        cb.showText(c);                    }                    cb.setTextMatrix(keepBarX + x * (11 + 12 * 7), textStartY);                    cb.showText(code.substring(11, 12));                    break;                case UPCE:                    cb.setTextMatrix(0, textStartY);                    cb.showText(code.substring(0, 1));                    for (int k = 1; k < 7; ++k) {                        String c = code.substring(k, k + 1);                        float len = font.getWidthPoint(c, size);                        float pX = keepBarX + TEXTPOS_EAN13[k - 1] * x - len / 2;                        cb.setTextMatrix(pX, textStartY);                        cb.showText(c);                    }                    cb.setTextMatrix(keepBarX + x * (9 + 6 * 7), textStartY);                    cb.showText(code.substring(7, 8));                    break;                case SUPP2:                case SUPP5:                    for (int k = 0; k < code.length(); ++k) {                        String c = code.substring(k, k + 1);                        float len = font.getWidthPoint(c, size);                        float pX = (7.5f + (9 * k)) * x - len / 2;                        cb.setTextMatrix(pX, textStartY);                        cb.showText(c);                    }                    break;            }            cb.endText();        }        return rect;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -