2 * Copyright (C) 2010 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android;
19 import java.util.Arrays;
20 import java.util.List;
21 import java.util.Vector;
22 import java.util.regex.Pattern;
24 import android.content.Intent;
25 import android.net.Uri;
26 import com.google.zxing.BarcodeFormat;
28 final class DecodeFormatManager {
30 private static final Pattern COMMA_PATTERN = Pattern.compile(",");
32 static final Vector<BarcodeFormat> PRODUCT_FORMATS;
33 static final Vector<BarcodeFormat> ONE_D_FORMATS;
34 static final Vector<BarcodeFormat> QR_CODE_FORMATS;
35 static final Vector<BarcodeFormat> DATA_MATRIX_FORMATS;
37 PRODUCT_FORMATS = new Vector<BarcodeFormat>(5);
38 PRODUCT_FORMATS.add(BarcodeFormat.UPC_A);
39 PRODUCT_FORMATS.add(BarcodeFormat.UPC_E);
40 PRODUCT_FORMATS.add(BarcodeFormat.EAN_13);
41 PRODUCT_FORMATS.add(BarcodeFormat.EAN_8);
42 PRODUCT_FORMATS.add(BarcodeFormat.RSS14);
43 ONE_D_FORMATS = new Vector<BarcodeFormat>(PRODUCT_FORMATS.size() + 4);
44 ONE_D_FORMATS.addAll(PRODUCT_FORMATS);
45 ONE_D_FORMATS.add(BarcodeFormat.CODE_39);
46 ONE_D_FORMATS.add(BarcodeFormat.CODE_93);
47 ONE_D_FORMATS.add(BarcodeFormat.CODE_128);
48 ONE_D_FORMATS.add(BarcodeFormat.ITF);
49 QR_CODE_FORMATS = new Vector<BarcodeFormat>(1);
50 QR_CODE_FORMATS.add(BarcodeFormat.QR_CODE);
51 DATA_MATRIX_FORMATS = new Vector<BarcodeFormat>(1);
52 DATA_MATRIX_FORMATS.add(BarcodeFormat.DATA_MATRIX);
55 private DecodeFormatManager() {}
57 static Vector<BarcodeFormat> parseDecodeFormats(Intent intent) {
58 List<String> scanFormats = null;
59 String scanFormatsString = intent.getStringExtra(Intents.Scan.SCAN_FORMATS);
60 if (scanFormatsString != null) {
61 scanFormats = Arrays.asList(COMMA_PATTERN.split(scanFormatsString));
63 return parseDecodeFormats(scanFormats, intent.getStringExtra(Intents.Scan.MODE));
66 static Vector<BarcodeFormat> parseDecodeFormats(Uri inputUri) {
67 List<String> formats = inputUri.getQueryParameters(Intents.Scan.SCAN_FORMATS);
68 if (formats != null && formats.size() == 1 && formats.get(0) != null){
69 formats = Arrays.asList(COMMA_PATTERN.split(formats.get(0)));
71 return parseDecodeFormats(formats, inputUri.getQueryParameter(Intents.Scan.MODE));
74 private static Vector<BarcodeFormat> parseDecodeFormats(Iterable<String> scanFormats,
76 if (scanFormats != null) {
77 Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>();
79 for (String format : scanFormats) {
80 formats.add(BarcodeFormat.valueOf(format));
83 } catch (IllegalArgumentException iae) {
87 if (decodeMode != null) {
88 if (Intents.Scan.PRODUCT_MODE.equals(decodeMode)) {
89 return PRODUCT_FORMATS;
91 if (Intents.Scan.QR_CODE_MODE.equals(decodeMode)) {
92 return QR_CODE_FORMATS;
94 if (Intents.Scan.DATA_MATRIX_MODE.equals(decodeMode)) {
95 return DATA_MATRIX_FORMATS;
97 if (Intents.Scan.ONE_D_MODE.equals(decodeMode)) {