]> Pileus Git - ~andy/freeotp/blob - src/org/fedorahosted/freeotp/DecodeAsyncTask.java
Add native camera support
[~andy/freeotp] / src / org / fedorahosted / freeotp / DecodeAsyncTask.java
1 /*
2  * FreeOTP
3  *
4  * Authors: Nathaniel McCallum <npmccallum@redhat.com>
5  *
6  * Copyright (C) 2013  Nathaniel McCallum, Red Hat
7  *
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
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  */
20
21 package org.fedorahosted.freeotp;
22
23 import java.util.concurrent.BlockingQueue;
24 import java.util.concurrent.LinkedBlockingQueue;
25
26 import android.hardware.Camera;
27 import android.hardware.Camera.PreviewCallback;
28 import android.os.AsyncTask;
29
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;
40
41 public class DecodeAsyncTask extends AsyncTask<Void, Void, String> implements PreviewCallback {
42         private static class Data {
43                 public byte[] data;
44                 Camera.Size size;
45         }
46
47         private final BlockingQueue<Data> mBlockingQueue;
48         private final Reader mReader;
49
50         public DecodeAsyncTask() {
51                 mBlockingQueue = new LinkedBlockingQueue<Data>(5);
52                 mReader = new QRCodeReader();
53         }
54
55         @Override
56         protected String doInBackground(Void... args) {
57                 while (true) {
58                         try {
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)));
64                                 return r.getText();
65                         } catch (InterruptedException e) {
66                                 return null;
67                         } catch (NotFoundException e) {
68                         } catch (ChecksumException e) {
69                         } catch (FormatException e) {
70                         } catch (ArrayIndexOutOfBoundsException e) {
71                         } finally {
72                                 mReader.reset();
73                         }
74                 }
75         }
76
77         @Override
78         public void onPreviewFrame(byte[] data, Camera camera) {
79                 Data d = new Data();
80                 d.data = data;
81                 d.size = camera.getParameters().getPreviewSize();
82                 mBlockingQueue.offer(d);
83         }
84 }