/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/CaptureActivity.java
Java | 118 lines | 72 code | 28 blank | 18 comment | 0 complexity | ac96d59e6b31557a60e30f15dd2ae1d9 MD5 | raw file
1/* 2 * Copyright (C) 2011 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * 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, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 17package com.googlecode.eyesfree.ocr.intent; 18 19import android.app.Activity; 20import android.graphics.Rect; 21import android.graphics.YuvImage; 22import android.hardware.Camera; 23import android.net.Uri; 24import android.os.Bundle; 25import android.util.Log; 26 27import com.googlecode.eyesfree.ocr.client.Intents; 28import com.googlecode.eyesfree.opticflow.FrameProducer.Frame; 29import com.googlecode.eyesfree.opticflow.FrameProducer.FrameReceiver; 30 31import java.io.IOException; 32import java.io.OutputStream; 33 34/** 35 * @author alanv@google.com (Alan Viverette) 36 */ 37public abstract class CaptureActivity extends CameraActivity { 38 private static final String TAG = "CaptureActivity"; 39 40 private static final int DEFAULT_JPEG_QUALITY = 85; 41 42 private Uri mSaveUri; 43 44 @Override 45 public void onCreate(Bundle savedInstanceState) { 46 super.onCreate(savedInstanceState); 47 48 mSaveUri = getIntent().getParcelableExtra(Intents.Detect.EXTRA_OUTPUT); 49 } 50 51 protected void takePreview() { 52 getCameraManager().requestFrame(frameCallback); 53 } 54 55 protected void takePicture() { 56 getCameraManager().requestPicture(pictureCallback); 57 } 58 59 private void writePreview(byte[] yuvData, int format, int width, int height) { 60 Log.i(TAG, "Writing preview to " + mSaveUri.toString()); 61 62 int result = Activity.RESULT_CANCELED; 63 64 try { 65 Rect rectangle = new Rect(0, 0, width, height); 66 67 OutputStream stream = getContentResolver().openOutputStream(mSaveUri); 68 69 YuvImage yuvImage = new YuvImage(yuvData, format, width, height, null); 70 yuvImage.compressToJpeg(rectangle, DEFAULT_JPEG_QUALITY, stream); 71 72 stream.close(); 73 74 result = Activity.RESULT_OK; 75 } catch (IOException e) { 76 e.printStackTrace(); 77 } 78 79 setResult(result); 80 finish(); 81 } 82 83 private void writePicture(byte[] jpegData) { 84 Log.i(TAG, "Writing picture to " + mSaveUri.toString()); 85 86 int result = Activity.RESULT_CANCELED; 87 88 try { 89 OutputStream stream = getContentResolver().openOutputStream(mSaveUri); 90 91 stream.write(jpegData); 92 stream.close(); 93 94 result = Activity.RESULT_OK; 95 } catch (IOException e) { 96 e.printStackTrace(); 97 } 98 99 setResult(result); 100 finish(); 101 } 102 103 private final FrameReceiver frameCallback = new FrameReceiver() { 104 @Override 105 public void onFrameReceived(Frame frame) { 106 writePreview(frame.data, frame.format, frame.width, frame.height); 107 108 frame.recycle(); 109 } 110 }; 111 112 private final Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { 113 @Override 114 public void onPictureTaken(byte[] data, Camera camera) { 115 writePicture(data); 116 } 117 }; 118}