• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2009 ZXing authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.zxing.oned;
18 
19 import java.util.Collection;
20 import java.util.Collections;
21 
22 import com.google.zxing.BarcodeFormat;
23 import com.google.zxing.FormatException;
24 import com.google.zxing.common.BitMatrix;
25 
26 /**
27  * This object renders an UPC-E code as a {@link BitMatrix}.
28  *
29  * @author 0979097955s@gmail.com (RX)
30  */
31 public final class UPCEWriter extends UPCEANWriter {
32 
33   private static final int CODE_WIDTH = 3 + // start guard
34       (7 * 6) + // bars
35       6; // end guard
36 
37   @Override
getSupportedWriteFormats()38   protected Collection<BarcodeFormat> getSupportedWriteFormats() {
39     return Collections.singleton(BarcodeFormat.UPC_E);
40   }
41 
42   @Override
encode(String contents)43   public boolean[] encode(String contents) {
44     int length = contents.length();
45     switch (length) {
46       case 7:
47         // No check digit present, calculate it and add it
48         int check;
49         try {
50           check = UPCEANReader.getStandardUPCEANChecksum(UPCEReader.convertUPCEtoUPCA(contents));
51         } catch (FormatException fe) {
52           throw new IllegalArgumentException(fe);
53         }
54         contents += check;
55         break;
56       case 8:
57         try {
58           if (!UPCEANReader.checkStandardUPCEANChecksum(UPCEReader.convertUPCEtoUPCA(contents))) {
59             throw new IllegalArgumentException("Contents do not pass checksum");
60           }
61         } catch (FormatException ignored) {
62           throw new IllegalArgumentException("Illegal contents");
63         }
64         break;
65       default:
66         throw new IllegalArgumentException(
67             "Requested contents should be 7 or 8 digits long, but got " + length);
68     }
69 
70     checkNumeric(contents);
71 
72     int firstDigit = Character.digit(contents.charAt(0), 10);
73     if (firstDigit != 0 && firstDigit != 1) {
74       throw new IllegalArgumentException("Number system must be 0 or 1");
75     }
76 
77     int checkDigit = Character.digit(contents.charAt(7), 10);
78     int parities = UPCEReader.NUMSYS_AND_CHECK_DIGIT_PATTERNS[firstDigit][checkDigit];
79     boolean[] result = new boolean[CODE_WIDTH];
80 
81     int pos = appendPattern(result, 0, UPCEANReader.START_END_PATTERN, true);
82 
83     for (int i = 1; i <= 6; i++) {
84       int digit = Character.digit(contents.charAt(i), 10);
85       if ((parities >> (6 - i) & 1) == 1) {
86         digit += 10;
87       }
88       pos += appendPattern(result, pos, UPCEANReader.L_AND_G_PATTERNS[digit], false);
89     }
90 
91     appendPattern(result, pos, UPCEANReader.END_PATTERN, false);
92 
93     return result;
94   }
95 
96 }
97