4 * Authors: Nathaniel McCallum <npmccallum@redhat.com>
6 * Copyright (C) 2013 Nathaniel McCallum, Red Hat
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
21 package org.fedorahosted.freeotp;
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.LinkedBlockingQueue;
26 import android.hardware.Camera;
27 import android.hardware.Camera.PreviewCallback;
28 import android.os.AsyncTask;
30 import com.google.zxing.BinaryBitmap;
31 import com.google.zxing.ChecksumException;
32 import com.google.zxing.FormatException;
33 import com.google.zxing.LuminanceSource;
34 import com.google.zxing.NotFoundException;
35 import com.google.zxing.PlanarYUVLuminanceSource;
36 import com.google.zxing.Reader;
37 import com.google.zxing.Result;
38 import com.google.zxing.common.HybridBinarizer;
39 import com.google.zxing.qrcode.QRCodeReader;
41 public class DecodeAsyncTask extends AsyncTask<Void, Void, String> implements PreviewCallback {
42 private static class Data {
47 private final BlockingQueue<Data> mBlockingQueue;
48 private final Reader mReader;
50 public DecodeAsyncTask() {
51 mBlockingQueue = new LinkedBlockingQueue<Data>(5);
52 mReader = new QRCodeReader();
56 protected String doInBackground(Void... args) {
59 Data data = mBlockingQueue.take();
60 LuminanceSource ls = new PlanarYUVLuminanceSource(data.data,
61 data.size.width, data.size.height, 0, 0,
62 data.size.width, data.size.height, false);
63 Result r = mReader.decode(new BinaryBitmap(new HybridBinarizer(ls)));
65 } catch (InterruptedException e) {
67 } catch (NotFoundException e) {
68 } catch (ChecksumException e) {
69 } catch (FormatException e) {
70 } catch (ArrayIndexOutOfBoundsException e) {
78 public void onPreviewFrame(byte[] data, Camera camera) {
81 d.size = camera.getParameters().getPreviewSize();
82 mBlockingQueue.offer(d);