making stuff final, weakening types, etc. per IntelliJ analysis
[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       public void handleMessage(Message message) {
52         switch (message.what) {
53           case R.id.save:
54             save((byte[]) message.obj, message.arg1, message.arg2);
55             break;
56           case R.id.quit:
57             Looper.myLooper().quit();
58             break;
59         }
60       }
61     };
62     Looper.loop();
63   }
64
65   // Save the center rectangle of the Y channel as a greyscale JPEG to the SD card
66   private void save(byte[] data, int width, int height) {
67     int framingWidth = mFramingRect.width();
68     int framingHeight = mFramingRect.height();
69     assert (framingWidth <= width);
70     assert (framingHeight <= height);
71
72     int leftOffset = mFramingRect.left;
73     int topOffset = mFramingRect.top;
74     int[] colors = new int[framingWidth * framingHeight];
75
76     for (int y = 0; y < framingHeight; y++) {
77       int rowOffset = (y + topOffset) * width + leftOffset;
78       for (int x = 0; x < framingWidth; x++) {
79         int pixel = (int) data[rowOffset + x];
80         pixel = 0xff000000 + (pixel << 16) + (pixel << 8) + pixel;
81         colors[y * framingWidth + x] = pixel;
82       }
83     }
84
85     Bitmap bitmap = Bitmap.createBitmap(colors, framingWidth, framingHeight,
86         Bitmap.Config.ARGB_8888);
87     OutputStream outStream = getNewPhotoOutputStream();
88     if (outStream != null) {
89       bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outStream);
90       try {
91         outStream.close();
92         Message message = Message.obtain(mActivity.mHandler, R.id.save_succeeded);
93         message.sendToTarget();
94         return;
95       } catch (IOException e) {
96         Log.e(TAG, "Exception closing stream: " + e.toString());
97       }
98     }
99
100     Message message = Message.obtain(mActivity.mHandler, R.id.save_failed);
101     message.sendToTarget();
102   }
103
104   private OutputStream getNewPhotoOutputStream() {
105     File sdcard = new File("/sdcard");
106     if (sdcard.exists()) {
107       File barcodes = new File(sdcard, "barcodes");
108       if (barcodes.exists()) {
109         if (!barcodes.isDirectory()) {
110           Log.e(TAG, "/sdcard/barcodes exists but is not a directory");
111           return null;
112         }
113       } else {
114         if (!barcodes.mkdir()) {
115           Log.e(TAG, "Could not create /sdcard/barcodes directory");
116           return null;
117         }
118       }
119       Date now = new Date();
120       String fileName = now.getTime() + ".jpg";
121       try {
122         return new FileOutputStream(new File(barcodes, fileName));
123       } catch (FileNotFoundException e) {
124         Log.e(TAG, "Could not create FileOutputStream");
125       }
126     } else {
127       Log.e(TAG, "/sdcard does not exist");
128     }
129     return null;
130   }
131
132 }