More reckless refactoring and code style tweaks -- mostly adding braces around condit...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / SaveThread.java
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.androidtest;
18
19 import android.graphics.Bitmap;
20 import android.graphics.Rect;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.Message;
24 import android.util.Log;
25
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.IOException;
30 import java.io.OutputStream;
31 import java.util.Date;
32
33 final class SaveThread extends Thread {
34
35   private static final String TAG = "SaveThread";
36
37   public Handler mHandler;
38
39   private final CameraTestActivity mActivity;
40   private final Rect mFramingRect;
41
42   SaveThread(CameraTestActivity activity, Rect framingRect) {
43     mActivity = activity;
44     mFramingRect = framingRect;
45   }
46
47   @Override
48   public void run() {
49     Looper.prepare();
50     mHandler = new Handler() {
51       @Override
52       public void handleMessage(Message message) {
53         switch (message.what) {
54           case R.id.save:
55             save((byte[]) message.obj, message.arg1, message.arg2);
56             break;
57           case R.id.quit:
58             Looper.myLooper().quit();
59             break;
60         }
61       }
62     };
63     Looper.loop();
64   }
65
66   // Save the center rectangle of the Y channel as a greyscale JPEG to the SD card
67   private void save(byte[] data, int width, int height) {
68     int framingWidth = mFramingRect.width();
69     int framingHeight = mFramingRect.height();
70     assert (framingWidth <= width);
71     assert (framingHeight <= height);
72
73     int leftOffset = mFramingRect.left;
74     int topOffset = mFramingRect.top;
75     int[] colors = new int[framingWidth * framingHeight];
76
77     for (int y = 0; y < framingHeight; y++) {
78       int rowOffset = (y + topOffset) * width + leftOffset;
79       for (int x = 0; x < framingWidth; x++) {
80         int pixel = (int) data[rowOffset + x];
81         pixel = 0xff000000 + (pixel << 16) + (pixel << 8) + pixel;
82         colors[y * framingWidth + x] = pixel;
83       }
84     }
85
86     Bitmap bitmap = Bitmap.createBitmap(colors, framingWidth, framingHeight,
87         Bitmap.Config.ARGB_8888);
88     OutputStream outStream = getNewPhotoOutputStream();
89     if (outStream != null) {
90       bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
91       try {
92         outStream.close();
93         Message message = Message.obtain(mActivity.mHandler, R.id.save_succeeded);
94         message.sendToTarget();
95         return;
96       } catch (IOException e) {
97         Log.e(TAG, "Exception closing stream: " + e.toString());
98       }
99     }
100
101     Message message = Message.obtain(mActivity.mHandler, R.id.save_failed);
102     message.sendToTarget();
103   }
104
105   private OutputStream getNewPhotoOutputStream() {
106     File sdcard = new File("/sdcard");
107     if (sdcard.exists()) {
108       File barcodes = new File(sdcard, "barcodes");
109       if (barcodes.exists()) {
110         if (!barcodes.isDirectory()) {
111           Log.e(TAG, "/sdcard/barcodes exists but is not a directory");
112           return null;
113         }
114       } else {
115         if (!barcodes.mkdir()) {
116           Log.e(TAG, "Could not create /sdcard/barcodes directory");
117           return null;
118         }
119       }
120       Date now = new Date();
121       String fileName = now.getTime() + ".jpg";
122       try {
123         return new FileOutputStream(new File(barcodes, fileName));
124       } catch (FileNotFoundException e) {
125         Log.e(TAG, "Could not create FileOutputStream");
126       }
127     } else {
128       Log.e(TAG, "/sdcard does not exist");
129     }
130     return null;
131   }
132
133 }