📄 barcodepdf417.java
字号:
/* * * Copyright 2003 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 java.awt.Canvas;import java.awt.Color;import java.awt.image.MemoryImageSource;import java.util.ArrayList;import com.lowagie.text.BadElementException;import com.lowagie.text.Image;import com.lowagie.text.pdf.codec.CCITTG4Encoder;/** Generates the 2D barcode PDF417. Supports dimensioning auto-sizing, fixed * and variable sizes, automatic and manual error levels, raw codeword input, * codeword size optimization and bitmap inversion. The output can * be a CCITT G4 <CODE>Image</CODE> or a raw bitmap. * @author Paulo Soares (psoares@consiste.pt) */public class BarcodePDF417 { /** Auto-size is made based on <CODE>aspectRatio</CODE> and <CODE>yHeight</CODE>. */ public static final int PDF417_USE_ASPECT_RATIO = 0; /** The size of the barcode will be at least <CODE>codeColumns*codeRows</CODE>. */ public static final int PDF417_FIXED_RECTANGLE = 1; /** The size will be at least <CODE>codeColumns</CODE> * with a variable number of <CODE>codeRows</CODE>. */ public static final int PDF417_FIXED_COLUMNS = 2; /** The size will be at least <CODE>codeRows</CODE> * with a variable number of <CODE>codeColumns</CODE>. */ public static final int PDF417_FIXED_ROWS = 4; /** The error level correction is set automatically according * to ISO 15438 recommendations. */ public static final int PDF417_AUTO_ERROR_LEVEL = 0; /** The error level correction is set by the user. It can be 0 to 8. */ public static final int PDF417_USE_ERROR_LEVEL = 16; /** * One single binary segment is used */ public static final int PDF417_FORCE_BINARY = 32; /** No <CODE>text</CODE> interpretation is done and the content of <CODE>codewords</CODE> * is used directly. */ public static final int PDF417_USE_RAW_CODEWORDS = 64; /** Inverts the output bits of the raw bitmap that is normally * bit one for black. It has only effect for the raw bitmap. */ public static final int PDF417_INVERT_BITMAP = 128; /** Use Macro PDF417 Encoding * @see #setMacroFileId(String) * @see #setMacroSegmentId(int) * @see #setMacroSegmentCount(int) */ public static final int PDF417_USE_MACRO = 256; private int macroSegmentCount=0; private int macroSegmentId=-1; private String macroFileId; private int macroIndex; protected int bitPtr; protected int cwPtr; protected SegmentList segmentList; /** Creates a new <CODE>BarcodePDF417</CODE> with the default settings. */ public BarcodePDF417() { setDefaultParameters(); } /** * Sets the segment id for macro PDF417 encoding * @param id the id (starting at 0) * @see #setMacroSegmentCount(int) */ public void setMacroSegmentId(int id) { this.macroSegmentId = id; } /** * Sets the segment count for macro PDF417 encoding * @param cnt the number of macro segments * @see #setMacroSegmentId(int) */ public void setMacroSegmentCount(int cnt) { this.macroSegmentCount = cnt; } /** * Sets the File ID for macro PDF417 encoding * @param id the file id */ public void setMacroFileId(String id) { this.macroFileId = id; } protected boolean checkSegmentType(Segment segment, char type) { if (segment == null) return false; return segment.type == type; } protected int getSegmentLength(Segment segment) { if (segment == null) return 0; return segment.end - segment.start; } /** Set the default settings that correspond to <CODE>PDF417_USE_ASPECT_RATIO</CODE> * and <CODE>PDF417_AUTO_ERROR_LEVEL</CODE>. */ public void setDefaultParameters() { options = 0; outBits = null; text = new byte[0]; yHeight = 3; aspectRatio = 0.5f; } protected void outCodeword17(int codeword) { int bytePtr = bitPtr / 8; int bit = bitPtr - bytePtr * 8; outBits[bytePtr++] |= codeword >> (9 + bit); outBits[bytePtr++] |= codeword >> (1 + bit); codeword <<= 8; outBits[bytePtr] |= codeword >> (1 + bit); bitPtr += 17; } protected void outCodeword18(int codeword) { int bytePtr = bitPtr / 8; int bit = bitPtr - bytePtr * 8; outBits[bytePtr++] |= codeword >> (10 + bit); outBits[bytePtr++] |= codeword >> (2 + bit); codeword <<= 8; outBits[bytePtr] |= codeword >> (2 + bit); if (bit == 7) outBits[++bytePtr] |= 0x80; bitPtr += 18; } protected void outCodeword(int codeword) { outCodeword17(codeword); } protected void outStopPattern() { outCodeword18(STOP_PATTERN); } protected void outStartPattern() { outCodeword17(START_PATTERN); } protected void outPaintCode() { int codePtr = 0; bitColumns = START_CODE_SIZE * (codeColumns + 3) + STOP_SIZE; int lenBits = ((bitColumns - 1) / 8 + 1) * codeRows; outBits = new byte[lenBits]; for (int row = 0; row < codeRows; ++row) { bitPtr = ((bitColumns - 1) / 8 + 1) * 8 * row; int rowMod = row % 3; int cluster[] = CLUSTERS[rowMod]; outStartPattern(); int edge = 0; switch (rowMod) { case 0: edge = 30 * (row / 3) + ((codeRows - 1) / 3); break; case 1: edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3); break; default: edge = 30 * (row / 3) + codeColumns - 1; break; } outCodeword(cluster[edge]); for (int column = 0; column < codeColumns; ++column) { outCodeword(cluster[codewords[codePtr++]]); } switch (rowMod) { case 0: edge = 30 * (row / 3) + codeColumns - 1; break; case 1: edge = 30 * (row / 3) + ((codeRows - 1) / 3); break; default: edge = 30 * (row / 3) + errorLevel * 3 + ((codeRows - 1) % 3); break; } outCodeword(cluster[edge]); outStopPattern(); } if ((options & PDF417_INVERT_BITMAP) != 0) { for (int k = 0; k < outBits.length; ++k) outBits[k] ^= 0xff; } } protected void calculateErrorCorrection(int dest) { if (errorLevel < 0 || errorLevel > 8) errorLevel = 0; int A[] = ERROR_LEVEL[errorLevel]; int Alength = 2 << errorLevel; for (int k = 0; k < Alength; ++k) codewords[dest + k] = 0; int lastE = Alength - 1; for (int k = 0; k < lenCodewords; ++k) { int t1 = codewords[k] + codewords[dest]; for (int e = 0; e <= lastE; ++e) { int t2 = (t1 * A[lastE - e]) % MOD; int t3 = MOD - t2; codewords[dest + e] = ((e == lastE ? 0 : codewords[dest + e + 1]) + t3) % MOD; } } for (int k = 0; k < Alength; ++k) codewords[dest + k] = (MOD - codewords[dest + k]) % MOD; } private static int getTextTypeAndValue(byte[] input, int maxLength, int idx) { if (idx >= maxLength) return 0; char c = (char)(input[idx] & 0xff); if (c >= 'A' && c <= 'Z') return (ALPHA + c - 'A'); if (c >= 'a' && c <= 'z') return (LOWER + c - 'a'); if (c == ' ') return (ALPHA + LOWER + MIXED + SPACE); int ms = MIXED_SET.indexOf(c); int ps = PUNCTUATION_SET.indexOf(c); if (ms < 0 && ps < 0) return (ISBYTE + c); if (ms == ps) return (MIXED + PUNCTUATION + ms); if (ms >= 0) return (MIXED + ms); return (PUNCTUATION + ps);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -