1 /* 2 * Copyright (C) 2008 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.client.android; 18 19 import com.google.zxing.BarcodeFormat; 20 import com.google.zxing.DecodeHintType; 21 import com.google.zxing.ResultPointCallback; 22 23 import android.os.Handler; 24 import android.os.Looper; 25 26 import java.util.Hashtable; 27 import java.util.Vector; 28 import java.util.concurrent.CountDownLatch; 29 30 /** 31 * This thread does all the heavy lifting of decoding the images. 32 * 33 * @author dswitkin@google.com (Daniel Switkin) 34 */ 35 final class DecodeThread extends Thread { 36 37 public static final String BARCODE_BITMAP = "barcode_bitmap"; 38 39 private final CaptureActivity activity; 40 private final Hashtable<DecodeHintType,Object> hints; 41 private Handler handler; 42 private final CountDownLatch handlerInitLatch; 43 DecodeThread(CaptureActivity activity, String characterSet, ResultPointCallback resultPointCallback)44 DecodeThread(CaptureActivity activity, 45 String characterSet, 46 ResultPointCallback resultPointCallback) { 47 48 this.activity = activity; 49 handlerInitLatch = new CountDownLatch(1); 50 51 hints = new Hashtable<DecodeHintType,Object>(); 52 Vector<BarcodeFormat> formats = new Vector<BarcodeFormat>(); 53 formats.add(BarcodeFormat.QR_CODE); 54 hints.put(DecodeHintType.POSSIBLE_FORMATS, formats); 55 56 if (characterSet != null) { 57 hints.put(DecodeHintType.CHARACTER_SET, characterSet); 58 } 59 hints.put(DecodeHintType.NEED_RESULT_POINT_CALLBACK, resultPointCallback); 60 } 61 getHandler()62 Handler getHandler() { 63 try { 64 handlerInitLatch.await(); 65 } catch (InterruptedException ie) { 66 // continue? 67 } 68 return handler; 69 } 70 71 @Override run()72 public void run() { 73 Looper.prepare(); 74 handler = new DecodeHandler(activity, hints); 75 handlerInitLatch.countDown(); 76 Looper.loop(); 77 } 78 79 } 80