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

📄 barcodedatamatrix.java

📁 iText是一个能够快速产生PDF文件的java类库。iText的java类对于那些要产生包含文本
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * $Id: BarcodeDatamatrix.java 2771 2007-05-21 10:53:31Z psoares33 $
 *
 * Copyright 2007 by Paulo Soares.
 *
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the License.
 *
 * The Original Code is 'iText, a free JAVA-PDF library'.
 *
 * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
 * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
 * All Rights Reserved.
 * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
 * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
 *
 * Contributor(s): all the names of the contributors are added in the source code
 * where applicable.
 *
 * Alternatively, the contents of this file may be used under the terms of the
 * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
 * provisions of LGPL are applicable instead of those above.  If you wish to
 * allow use of your version of this file only under the terms of the LGPL
 * License and not to allow others to use your version of this file under
 * the MPL, indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by the LGPL.
 * If you do not delete the provisions above, a recipient may use your version
 * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the MPL as stated above or under the terms of the GNU
 * Library General Public License as published by the Free Software Foundation;
 * either version 2 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
 * details.
 *
 * If you didn't download this code from the following link, you should check if
 * you aren't using an obsolete version:
 * http://www.lowagie.com/iText/
 */
package com.lowagie.text.pdf;

import com.lowagie.text.BadElementException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.codec.CCITTG4Encoder;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.image.MemoryImageSource;
import java.util.Arrays;
import java.io.UnsupportedEncodingException;
import java.util.Hashtable;

/**
 * A DataMatrix 2D barcode generator.
 */
public class BarcodeDatamatrix {
    /**
     * No error.
     */
    public static final int DM_NO_ERROR = 0;
    /**
     * The text is too big for the symbology capabilities.
     */
    public static final int DM_ERROR_TEXT_TOO_BIG = 1;
    /**
     * The dimensions given for the symbol are illegal.
     */
    public static final int DM_ERROR_INVALID_SQUARE = 3;
    /**
     * An error while parsing an extension.
     */
    public static final int DM_ERROR_EXTENSION = 5;

    /**
     * The best encodation will be used.
     */
    public static final int DM_AUTO = 0;
    /**
     * ASCII encodation.
     */
    public static final int DM_ASCII = 1;
    /**
     * C40 encodation.
     */
    public static final int DM_C40 = 2;
    /**
     * TEXT encodation.
     */
    public static final int DM_TEXT = 3;
    /**
     * Binary encodation.
     */
    public static final int DM_B256 = 4;
    /**
     * X21 encodation.
     */
    public static final int DM_X21 = 5;
    /**
     * EDIFACT encodation.
     */
    public static final int DM_EDIFACT = 6;
    /**
     * No encodation needed. The bytes provided are already encoded.
     */
    public static final int DM_RAW = 7;

    /**
     * Allows extensions to be embedded at the start of the text.
     */
    public static final int DM_EXTENSION = 32;
    /**
     * Doesn't generate the image but returns all the other information.
     */
    public static final int DM_TEST = 64;

    private final static DmParams[] dmSizes = {
        new DmParams(10, 10, 10, 10, 3, 3, 5),
        new DmParams(12, 12, 12, 12, 5, 5, 7),
        new DmParams(8, 18, 8, 18, 5, 5, 7),
        new DmParams(14, 14, 14, 14, 8, 8, 10),
        new DmParams(8, 32, 8, 16, 10, 10, 11),
        new DmParams(16, 16, 16, 16, 12, 12, 12),
        new DmParams(12, 26, 12, 26, 16, 16, 14),
        new DmParams(18, 18, 18, 18, 18, 18, 14),
        new DmParams(20, 20, 20, 20, 22, 22, 18),
        new DmParams(12, 36, 12, 18, 22, 22, 18),
        new DmParams(22, 22, 22, 22, 30, 30, 20),
        new DmParams(16, 36, 16, 18, 32, 32, 24),
        new DmParams(24, 24, 24, 24, 36, 36, 24),
        new DmParams(26, 26, 26, 26, 44, 44, 28),
        new DmParams(16, 48, 16, 24, 49, 49, 28),
        new DmParams(32, 32, 16, 16, 62, 62, 36),
        new DmParams(36, 36, 18, 18, 86, 86, 42),
        new DmParams(40, 40, 20, 20, 114, 114, 48),
        new DmParams(44, 44, 22, 22, 144, 144, 56),
        new DmParams(48, 48, 24, 24, 174, 174, 68),
        new DmParams(52, 52, 26, 26, 204, 102, 42),
        new DmParams(64, 64, 16, 16, 280, 140, 56),
        new DmParams(72, 72, 18, 18, 368, 92, 36),
        new DmParams(80, 80, 20, 20, 456, 114, 48),
        new DmParams(88, 88, 22, 22, 576, 144, 56),
        new DmParams(96, 96, 24, 24, 696, 174, 68),
        new DmParams(104, 104, 26, 26, 816, 136, 56),
        new DmParams(120, 120, 20, 20, 1050, 175, 68),
        new DmParams(132, 132, 22, 22, 1304, 163, 62),
        new DmParams(144, 144, 24, 24, 1558, 156, 62)};

    private static final String x12 = "\r*> 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private int extOut;
    private short[] place;
    private byte[] image;
    private int height;
    private int width;
    private int ws;
    private int options;
    
    /**
     * Creates an instance of this class.
     */
    public BarcodeDatamatrix() {
    }

    private void setBit(int x, int y, int xByte) {
        image[y * xByte + x / 8] |= (byte)(128 >> (x & 7));
    }

    private void draw(byte[] data, int dataSize, DmParams dm) {
        int i, j, p, x, y, xs, ys, z;
        int xByte = (dm.width + ws * 2 + 7) / 8;
        Arrays.fill(image, (byte)0);
        //alignment patterns
        //dotted horizontal line
        for (i = ws; i < dm.height + ws; i += dm.heightSection) {
            for (j = ws; j < dm.width + ws; j += 2) {
                setBit(j, i, xByte);
            }
        }
        //solid horizontal line
        for (i = dm.heightSection - 1 + ws; i < dm.height + ws; i += dm.heightSection) {
            for (j = ws; j < dm.width + ws; ++j) {
                setBit(j, i, xByte);
            }
        }
        //solid vertical line
        for (i = ws; i < dm.width + ws; i += dm.widthSection) {
            for (j = ws; j < dm.height + ws; ++j) {
                setBit(i, j, xByte);
            }
        }
        //dotted vertical line
        for (i = dm.widthSection - 1 + ws; i < dm.width + ws; i += dm.widthSection) {
            for (j = 1 + ws; j < dm.height + ws; j += 2) {
                setBit(i, j, xByte);
            }
        }
        p = 0;
        for (ys = 0; ys < dm.height; ys += dm.heightSection) {
            for (y = 1; y < dm.heightSection - 1; ++y) {
                for (xs = 0; xs < dm.width; xs += dm.widthSection) {
                    for (x = 1; x < dm.widthSection - 1; ++x) {
                        z = place[p++];
                        if (z == 1 || (z > 1 && ((data[z/8-1] & 0xff) & (128 >> (z%8))) != 0))
                            setBit(x + xs + ws, y + ys + ws, xByte);
                    }
                }
            }
        }
    }

    private static void makePadding(byte[] data, int position, int count) {
        //already in ascii mode
        if (count <= 0)
            return;
        data[position++] = (byte)129;
        while (--count > 0) {
            int t = 129 + (((position + 1) * 149) % 253) + 1;
            if (t > 254)
                t -= 254;
            data[position++] = (byte)t;
        }
    }

    private static boolean isDigit(int c) {
        return c >= '0' && c <= '9';
    }
    
    private static int asciiEncodation(byte[] text, int textOffset, int textLength, byte[] data, int dataOffset, int dataLength) {
        int ptrIn, ptrOut, c;
        ptrIn = textOffset;
        ptrOut = dataOffset;
        textLength += textOffset;
        dataLength += dataOffset;
        while (ptrIn < textLength) {
            if (ptrOut >= dataLength)
                return -1;
            c = text[ptrIn++] & 0xff;
            if (isDigit(c) && ptrIn < textLength && isDigit(text[ptrIn] & 0xff)) {
                data[ptrOut++] = (byte)((c - '0') * 10 + (text[ptrIn++] & 0xff) - '0' + 130);
            }
            else if (c > 127) {
                if (ptrOut + 1 >= dataLength)
                    return -1;
                data[ptrOut++] = (byte)235;
                data[ptrOut++] = (byte)(c - 128 + 1);
            }
            else {
                data[ptrOut++] = (byte)(c + 1);
            }
        }
        return ptrOut - dataOffset;
    }

    private static int b256Encodation(byte[] text, int textOffset, int textLength, byte[] data, int dataOffset, int dataLength) {
        int k, j, prn, tv, c;
        if (textLength == 0)
            return 0;
        if (textLength < 250 && textLength + 2 > dataLength)
            return -1;
        if (textLength >= 250 && textLength + 3 > dataLength)
            return -1;
        data[dataOffset] = (byte)231;
        if (textLength < 250) {
            data[dataOffset + 1] = (byte)textLength;
            k = 2;
        }
        else {
            data[dataOffset + 1] = (byte)(textLength / 250 + 249);
            data[dataOffset + 2] = (byte)(textLength % 250);
            k = 3;
        }
        System.arraycopy(text, textOffset, data, k + dataOffset, textLength);
        k += textLength + dataOffset;
        for (j = dataOffset + 1; j < k; ++j) {
            c = data[j] & 0xff;
            prn = ((149 * (j + 1)) % 255) + 1;
            tv = c + prn;
            if (tv > 255)
                tv -= 256;
            data[j] = (byte)tv;

        }
        return k - dataOffset;
    }

    private static int X12Encodation(byte[] text, int textOffset, int textLength, byte[] data, int dataOffset, int dataLength) {
        int ptrIn, ptrOut, count, k, n, ci;
        byte c;
        if (textLength == 0)
            return 0;
        ptrIn = 0;
        ptrOut = 0;
        byte[] x = new byte[textLength];
        count = 0;
        for (; ptrIn < textLength; ++ptrIn) {
            int i = x12.indexOf((char)text[ptrIn + textOffset]);
            if (i >= 0) {
                x[ptrIn] = (byte)i;
                ++count;
            }
            else {
                x[ptrIn] = 100;
                if (count >= 6)
                    count -= (count / 3) * 3;
                for (k = 0; k < count; ++k)
                    x[ptrIn - k - 1] = 100;
                count = 0;
            }
        }
        if (count >= 6)
            count -= (count / 3) * 3;
        for (k = 0; k < count; ++k)
            x[ptrIn - k - 1] = 100;
        ptrIn = 0;
        c = 0;

⌨️ 快捷键说明

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