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 CameraTestActivity mActivity;
40 private Rect mFramingRect;
42 SaveThread(CameraTestActivity activity, Rect framingRect) {
44 mFramingRect = framingRect;
50 mHandler = new Handler() {
51 public void handleMessage(Message message) {
52 switch (message.what) {
54 save((byte[]) message.obj, message.arg1, message.arg2);
57 Looper.myLooper().quit();
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);
72 int leftOffset = mFramingRect.left;
73 int topOffset = mFramingRect.top;
74 int[] colors = new int[framingWidth * framingHeight];
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;
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);
92 Message message = Message.obtain(mActivity.mHandler, R.id.save_succeeded);
93 message.sendToTarget();
95 } catch (IOException e) {
96 Log.e(TAG, "Exception closing stream: " + e.toString());
100 Message message = Message.obtain(mActivity.mHandler, R.id.save_failed);
101 message.sendToTarget();
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");
114 if (!barcodes.mkdir()) {
115 Log.e(TAG, "Could not create /sdcard/barcodes directory");
119 Date now = new Date();
120 String fileName = now.getTime() + ".jpg";
122 return new FileOutputStream(new File(barcodes, fileName));
123 } catch (FileNotFoundException e) {
124 Log.e(TAG, "Could not create FileOutputStream");
127 Log.e(TAG, "/sdcard does not exist");