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

📄 datamatrixhighlevelencoder.java

📁 生成二维条形码的java程序
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
                if ((count % 3) == 0) {
                    int newMode = lookAheadTest(context.msg, context.pos, getEncodingMode());
                    if (newMode != getEncodingMode()) {
                        context.signalEncoderChange(newMode);
                        break;
                    }
                }
            }
            handleEOD(context, buffer, lastCharSize);
        }

        private int backtrackOneCharacter(EncoderContext context, 
                StringBuffer buffer, StringBuffer removed, int lastCharSize) {
            int count = buffer.length();
            buffer.delete(count - lastCharSize, count);
            context.pos--;
            char c = context.getCurrentChar();
            lastCharSize = encodeChar(c, removed);
            context.resetSymbolInfo(); //Deal with possible reduction in symbol size
            return lastCharSize;
        }

        protected void writeNextTriplet(EncoderContext context, StringBuffer buffer) {
            context.writeCodewords(encodeToCodewords(buffer, 0));
            buffer.delete(0, 3);
        }
        
        /**
         * Handle "end of data" situations
         * @param context the encoder context
         * @param buffer the buffer with the remaining encoded characters
         */
        protected void handleEOD(EncoderContext context, StringBuffer buffer, int lastCharSize) {
            int unwritten = (buffer.length() / 3) * 2;
            int rest = buffer.length() % 3;
            
            int curCodewordCount = context.getCodewordCount() + unwritten;
            context.updateSymbolInfo(curCodewordCount);
            int available = context.symbolInfo.dataCapacity - curCodewordCount;
                
            if (rest == 2) {
                buffer.append('\0'); //Shift 1
                while (buffer.length() >= 3) {
                    writeNextTriplet(context, buffer);
                }
                if (context.hasMoreCharacters()) {
                    context.writeCodeword(C40_UNLATCH);
                }
            } else if (available == 1 && rest == 1) {
                while (buffer.length() >= 3) {
                    writeNextTriplet(context, buffer);
                }
                if (context.hasMoreCharacters()) {
                    context.writeCodeword(C40_UNLATCH);
                } else {
                    //No unlatch
                }
                context.pos--;
            } else if (rest == 0) {
                while (buffer.length() >= 3) {
                    writeNextTriplet(context, buffer);
                }
                if (available > 0 || context.hasMoreCharacters()) {
                    context.writeCodeword(C40_UNLATCH);
                }
            } else {
                throw new IllegalStateException("Unexpected case. Please report!");
            }
            context.signalEncoderChange(ASCII_ENCODATION);
        }
        
        protected int encodeChar(char c, StringBuffer sb) {
            if (c == ' ') {
                sb.append('\3');
                return 1;
            } else if (c >= '0' && c <= '9') {
                sb.append((char)(c - 48 + 4));
                return 1;
            } else if (c >= 'A' && c <= 'Z') {
                sb.append((char)(c - 65 + 14));
                return 1;
            } else if (c >= '\0' && c <= '\u001f') {
                sb.append('\0'); //Shift 1 Set
                sb.append(c);
                return 2;
            } else if (c >= '!' && c <= '/') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 33));
                return 2;
            } else if (c >= ':' && c <= '@') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 58 + 15));
                return 2;
            } else if (c >= '[' && c <= '_') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 91 + 22));
                return 2;
            } else if (c >= '\'' && c <= '\u007f') {
                sb.append('\2'); //Shift 3 Set
                sb.append((char)(c - 96));
                return 2;
            } else if (c >= '\u0080') {
                sb.append("\1\u001e"); //Shift 2, Upper Shift
                int len = 2;
                len += encodeChar((char)(c - 128), sb);
                return len;
            } else {
                throw new IllegalArgumentException("Illegal character: " + c);
            }
        }
        
        protected String encodeToCodewords(StringBuffer sb, int startPos) {
            char c1 = sb.charAt(startPos);
            char c2 = sb.charAt(startPos + 1);
            char c3 = sb.charAt(startPos + 2);
            int v = (1600 * c1) + (40 * c2) + c3 + 1;
            char cw1 = (char)(v / 256);
            char cw2 = (char)(v % 256);
            return "" + cw1 + cw2; 
        }

    }
    
    private static class TextEncoder extends C40Encoder {
        
        public int getEncodingMode() {
            return TEXT_ENCODATION;
        }
        
        protected int encodeChar(char c, StringBuffer sb) {
            if (c == ' ') {
                sb.append('\3');
                return 1;
            } else if (c >= '0' && c <= '9') {
                sb.append((char)(c - 48 + 4));
                return 1;
            } else if (c >= 'a' && c <= 'z') {
                sb.append((char)(c - 97 + 14));
                return 1;
            } else if (c >= '\0' && c <= '\u001f') {
                sb.append('\0'); //Shift 1 Set
                sb.append(c);
                return 2;
            } else if (c >= '!' && c <= '/') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 33));
                return 2;
            } else if (c >= ':' && c <= '@') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 58 + 15));
                return 2;
            } else if (c >= '[' && c <= '_') {
                sb.append('\1'); //Shift 2 Set
                sb.append((char)(c - 91 + 22));
                return 2;
            } else if (c == '\'') {
                sb.append('\2'); //Shift 3 Set
                sb.append((char)(c - 96));
                return 2;
            } else if (c >= 'A' && c <= 'Z') {
                sb.append('\2'); //Shift 3 Set
                sb.append((char)(c - 65 + 1));
                return 2;
            } else if (c >= '{' && c <= '\u007f') {
                sb.append('\2'); //Shift 3 Set
                sb.append((char)(c - 123 + 27));
                return 2;
            } else if (c >= '\u0080') {
                sb.append("\1\u001e"); //Shift 2, Upper Shift
                int len = 2;
                len += encodeChar((char)(c - 128), sb);
                return len;
            } else {
                throw new IllegalArgumentException("Illegal character: " + c);
            }
        }
        
    }
    
    private static class X12Encoder extends C40Encoder {
        
        public int getEncodingMode() {
            return X12_ENCODATION;
        }
        
        public void encode(EncoderContext context) {
            //step C
            StringBuffer buffer = new StringBuffer();
            while (context.hasMoreCharacters()) {
                char c = context.getCurrentChar();
                context.pos++;

                encodeChar(c, buffer);
                
                int count = buffer.length(); 
                if ((count % 3) == 0) {
                    writeNextTriplet(context, buffer);

                    int newMode = lookAheadTest(context.msg, context.pos, getEncodingMode());
                    if (newMode != getEncodingMode()) {
                        context.signalEncoderChange(newMode);
                        break;
                    }
                }
            }
            handleEOD(context, buffer);
        }

        protected int encodeChar(char c, StringBuffer sb) {
            if (c == '\r') {
                sb.append('\0');
            } else if (c == '*') {
                sb.append('\1');
            } else if (c == '>') {
                sb.append('\2');
            } else if (c == ' ') {
                sb.append('\3');
            } else if (c >= '0' && c <= '9') {
                sb.append((char)(c - 48 + 4));
            } else if (c >= 'A' && c <= 'Z') {
                sb.append((char)(c - 65 + 14));
            } else {
                throw new IllegalArgumentException("Illegal character: " + c);
            }
            return 1;
        }
        
        protected void handleEOD(EncoderContext context, StringBuffer buffer) {
            context.updateSymbolInfo();
            int available = context.symbolInfo.dataCapacity - context.getCodewordCount();
            int count = buffer.length();
            if (count == 2) {
                context.writeCodeword(X12_UNLATCH);
                context.pos -= 2;
                context.signalEncoderChange(ASCII_ENCODATION);
            } else if (count == 1) {
                context.pos--;
                if (available > 1) {
                    context.writeCodeword(X12_UNLATCH);
                } else {
                    //NOP - No unlatch necessary
                }
                context.signalEncoderChange(ASCII_ENCODATION);
            }
        }
    }
    
    private static class EdifactEncoder implements Encoder {
        
        public int getEncodingMode() {
            return EDIFACT_ENCODATION;
        }
        
        public void encode(EncoderContext context) {
            //step F
            StringBuffer buffer = new StringBuffer();
            while (context.hasMoreCharacters()) {
                char c = context.getCurrentChar();
                encodeChar(c, buffer);
                context.pos++;

                int count = buffer.length(); 
                if (count >= 4) {
                    context.writeCodewords(encodeToCodewords(buffer, 0));
                    buffer.delete(0, 4);

                    int newMode = lookAheadTest(context.msg, context.pos, getEncodingMode());
                    if (newMode != getEncodingMode()) {
                        context.signalEncoderChange(ASCII_ENCODATION);
                        break;
                    }
                }
            }
            buffer.append((char)31); //Unlatch
            handleEOD(context, buffer);
        }

        /**
         * Handle "end of data" situations
         * @param context the encoder context
         * @param buffer the buffer with the remaining encoded characters
         */
        protected void handleEOD(EncoderContext context, StringBuffer buffer) {
            try {
                int count = buffer.length();
                if (count == 0) {
                    return; //Already finished
                } else if (count == 1) {
                    //Only an unlatch at the end
                    context.updateSymbolInfo();
                    int available = context.symbolInfo.dataCapacity - context.getCodewordCount();
                    int remaining = context.getRemainingCharacters();
                    if (remaining == 0 && available <= 2) {
                        return; //No unlatch
                    }
                }

                if (count > 4) {
                    throw new IllegalStateException("Count must not exceed 4");
                }
                int restChars = count - 1;
                String encoded = encodeToCodewords(buffer, 0);
                boolean endOfSymbolReached = !context.hasMoreCharacters();
                boolean restInAscii = endOfSymbolReached && restChars <= 2;
                
                int available;
                if (restChars <= 2) {
                    context.updateSymbolInfo(context.getCodewordCount() + restChars);
                    available = context.symbolInfo.dataCapacity - context.getCodewordCount();
                    if (available >= 3) {
                        restInAscii = false;
                        context.updateSymbolInfo(context.getCodewordCount() + encoded.length());
                        available = context.symbolInfo.dataCapacity - context.getCodewordCount();
                    }
                }
                
                if (restInAscii) {
                    context.resetSymbolInfo();
                    context.pos -= restChars;
                } else {
                    context.writeCodewords(encoded);
                }
            } finally {
                context.signalEncoderChange(ASCII_ENCODATION);
            }    
        }
        
        protected void encodeChar(char c, StringBuffer sb) {
            if (c >= ' ' && c <= '?') {
                sb.append(c);
            } else if (c >= '@' && c <= '^') {
                sb.append((char)(c - 64));
            } else {
                throw new IllegalArgumentException("Illegal character: " + c);
            }
        }
        
        protected String encodeToCodewords(StringBuffer sb, int startPos) {
            int len = sb.length() - startPos;
            if (len == 0) {
                throw new IllegalStateException("StringBuffer must not be empty");
            }
            char c1 = sb.charAt(startPos);
            char c2 = (len >= 2 ? sb.charAt(startPos + 1) : 0);
            char c3 = (len >= 3 ? sb.charAt(startPos + 2) : 0);
            char c4 = (len >= 4 ? sb.charAt(startPos + 3) : 0);

⌨️ 快捷键说明

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