/ocr/ocrservice/src/com/googlecode/eyesfree/ocr/intent/CaptureActivity.java

http://eyes-free.googlecode.com/ · 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. package com.googlecode.eyesfree.ocr.intent;
  17. import android.app.Activity;
  18. import android.graphics.Rect;
  19. import android.graphics.YuvImage;
  20. import android.hardware.Camera;
  21. import android.net.Uri;
  22. import android.os.Bundle;
  23. import android.util.Log;
  24. import com.googlecode.eyesfree.ocr.client.Intents;
  25. import com.googlecode.eyesfree.opticflow.FrameProducer.Frame;
  26. import com.googlecode.eyesfree.opticflow.FrameProducer.FrameReceiver;
  27. import java.io.IOException;
  28. import java.io.OutputStream;
  29. /**
  30. * @author alanv@google.com (Alan Viverette)
  31. */
  32. public abstract class CaptureActivity extends CameraActivity {
  33. private static final String TAG = "CaptureActivity";
  34. private static final int DEFAULT_JPEG_QUALITY = 85;
  35. private Uri mSaveUri;
  36. @Override
  37. public void onCreate(Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. mSaveUri = getIntent().getParcelableExtra(Intents.Detect.EXTRA_OUTPUT);
  40. }
  41. protected void takePreview() {
  42. getCameraManager().requestFrame(frameCallback);
  43. }
  44. protected void takePicture() {
  45. getCameraManager().requestPicture(pictureCallback);
  46. }
  47. private void writePreview(byte[] yuvData, int format, int width, int height) {
  48. Log.i(TAG, "Writing preview to " + mSaveUri.toString());
  49. int result = Activity.RESULT_CANCELED;
  50. try {
  51. Rect rectangle = new Rect(0, 0, width, height);
  52. OutputStream stream = getContentResolver().openOutputStream(mSaveUri);
  53. YuvImage yuvImage = new YuvImage(yuvData, format, width, height, null);
  54. yuvImage.compressToJpeg(rectangle, DEFAULT_JPEG_QUALITY, stream);
  55. stream.close();
  56. result = Activity.RESULT_OK;
  57. } catch (IOException e) {
  58. e.printStackTrace();
  59. }
  60. setResult(result);
  61. finish();
  62. }
  63. private void writePicture(byte[] jpegData) {
  64. Log.i(TAG, "Writing picture to " + mSaveUri.toString());
  65. int result = Activity.RESULT_CANCELED;
  66. try {
  67. OutputStream stream = getContentResolver().openOutputStream(mSaveUri);
  68. stream.write(jpegData);
  69. stream.close();
  70. result = Activity.RESULT_OK;
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. setResult(result);
  75. finish();
  76. }
  77. private final FrameReceiver frameCallback = new FrameReceiver() {
  78. @Override
  79. public void onFrameReceived(Frame frame) {
  80. writePreview(frame.data, frame.format, frame.width, frame.height);
  81. frame.recycle();
  82. }
  83. };
  84. private final Camera.PictureCallback pictureCallback = new Camera.PictureCallback() {
  85. @Override
  86. public void onPictureTaken(byte[] data, Camera camera) {
  87. writePicture(data);
  88. }
  89. };
  90. }