?? pdf417lib.java
字號:
/* * Copyright 2003-2005 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 'pdf417lib, a library to generate the bidimensional barcode PDF417'. * * The Initial Developer of the Original Code is Paulo Soares. Portions created by * the Initial Developer are Copyright (C) 2003 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://sourceforge.net/projects/pdf417lib * This code is also used in iText (http://www.lowagie.com/iText) */import java.util.ArrayList;import java.io.UnsupportedEncodingException;/** 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 Pdf417lib { /** 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 recomendations. */ 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; /** 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; protected int bitPtr; protected int cwPtr; protected SegmentList segmentList; /** Creates a new <CODE>BarcodePDF417</CODE> with the default settings. */ public Pdf417lib() { setDefaultParameters(); } 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; } protected int getTextTypeAndValue(int maxLength, int idx) { if (idx >= maxLength) return 0; char c = (char)(text[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); } protected void textCompaction(int start, int length) { int dest[] = new int[ABSOLUTE_MAX_TEXT_SIZE * 2]; int mode = ALPHA; int ptr = 0; int fullBytes = 0; int v = 0; int k; int size; length += start; for (k = start; k < length; ++k) { v = getTextTypeAndValue(length, k); if ((v & mode) != 0) { dest[ptr++] = v & 0xff; continue; } if ((v & ISBYTE) != 0) { if ((ptr & 1) != 0) { dest[ptr++] = (mode & PUNCTUATION) != 0 ? PAL : PS; mode = (mode & PUNCTUATION) != 0 ? ALPHA : mode; } dest[ptr++] = BYTESHIFT; dest[ptr++] = v & 0xff; fullBytes += 2; continue; } switch (mode) { case ALPHA: if ((v & LOWER) != 0) { dest[ptr++] = LL; dest[ptr++] = v & 0xff; mode = LOWER;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -