2 * Copyright (C) 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.androidtest;
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;
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;
33 final class SaveThread extends Thread {
35 private static final String TAG = "SaveThread";
37 public Handler mHandler;
39 private final CameraTestActivity mActivity;
40 private final Rect mFramingRect;
42 SaveThread(CameraTestActivity activity, Rect framingRect) {
44 mFramingRect = framingRect;
50 mHandler = new Handler() {
52 public void handleMessage(Message message) {
53 switch (message.what) {
55 save((byte[]) message.obj, message.arg1, message.arg2);
58 Looper.myLooper().quit();
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);
73 int leftOffset = mFramingRect.left;
74 int topOffset = mFramingRect.top;
75 int[] colors = new int[framingWidth * framingHeight];
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;
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);
93 Message message = Message.obtain(mActivity.mHandler, R.id.save_succeeded);
94 message.sendToTarget();
96 } catch (IOException e) {
97 Log.e(TAG, "Exception closing stream: " + e.toString());
101 Message message = Message.obtain(mActivity.mHandler, R.id.save_failed);
102 message.sendToTarget();
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");
115 if (!barcodes.mkdir()) {
116 Log.e(TAG, "Could not create /sdcard/barcodes directory");
120 Date now = new Date();
121 String fileName = now.getTime() + ".jpg";
123 return new FileOutputStream(new File(barcodes, fileName));
124 } catch (FileNotFoundException e) {
125 Log.e(TAG, "Could not create FileOutputStream");
128 Log.e(TAG, "/sdcard does not exist");