2013-05-12 51 views
5

我試圖構建一個應用程序,每次手機解鎖時都會捕獲照片。用戶不應該看到照片已被拍攝。所以它應該基本上像一個防盜應用程序。Android偷偷解鎖照片

我讀過我應該使用虛擬SurfaceTexture,所以預覽不顯示在屏幕上,但我無法捕捉照片。

這是我迄今寫的。這僅僅是一個樣品活動取得聯繫與Camera對象:

package com.brushmate.chameleon; 

import java.io.IOException; 

import android.graphics.SurfaceTexture; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.util.Log; 
import android.view.SurfaceView; 

public class SettingsActivity extends Activity { 
    private static final String TAG = "Chameleon Wallpaper"; 

    @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     Log.d(TAG, "Activity created"); 

     Camera cam = getCamera(); 

     if (cam != null) { 
      Log.d(TAG, "Camera available"); 

      SurfaceTexture dummy = new SurfaceTexture(0); 

      try { 
      cam.setPreviewTexture(dummy); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Log.d(TAG, "Preview texture set"); 

      cam.startPreview(); 

      Log.d(TAG, "Preview started"); 

      cam.takePicture(null, null, new Camera.PictureCallback() { 

       @Override 
       public void onPictureTaken(byte[] data, Camera camera) { 
        Log.d(TAG, "Image taken"); 
       } 
      }); 

      cam.stopPreview(); 

      Log.d(TAG, "Preview stopped"); 

      cam.release(); 

      Log.d(TAG, "Camera released"); 
     } 
    } 

    private Camera getCamera() { 
     Camera cam = null; 

     try { 
      cam = Camera.open(); 
     } catch (RuntimeException e) { 
      Log.e(TAG, "Camera not available", e); 
     } 

     return cam; 
    } 

} 

這裏是我的logcat輸出:

05-12 23:54:56.947: D/Chameleon Wallpaper(19614): Activity created 
05-12 23:54:56.947: I/AwesomePlayer(161): setDataSource_l(URL suppressed) 
05-12 23:54:56.977: I/AwesomePlayer(161): setDataSource_l(URL suppressed) 
05-12 23:54:57.007: I/CameraClient(161): Opening camera 0 
05-12 23:54:57.117: E/mm-camera(175): sensor_load_chromatix: libchromatix_imx111_preview.so: 30 
05-12 23:54:57.217: E/mm-camera(175): vfe_ops_init: E 
05-12 23:54:57.237: E/mm-camera(175): vfe_legacy_stats_buffer_init: AEC_STATS_BUFNUM 
05-12 23:54:57.237: E/mm-camera(175): vfe_legacy_stats_buffer_init: AEC_STATS_BUFNUM 
05-12 23:54:57.247: E/mm-camera(175): mctl_init_stats_proc_info: snap_max_line_cnt =30096 
05-12 23:54:57.267: D/Chameleon Wallpaper(19614): Camera available 
05-12 23:54:57.267: D/Chameleon Wallpaper(19614): Preview texture set 
05-12 23:54:57.267: E/QCameraHWI(161): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*): mPreviewWindow = 0x0x400b9e98, mStreamDisplay = 0x0x4008c378 
05-12 23:54:57.267: D/QCameraHWI(161): android::status_t android::QCameraHardwareInterface::startPreview(): start preview now 
05-12 23:54:57.267: I/QCameraHWI(161): android::status_t android::QCameraHardwareInterface::startPreview2():Setting ZSL mode 
05-12 23:54:57.267: E/mm-camera(175): config_proc_CAMERA_SET_INFORM_STARTPREVIEW 
05-12 23:54:57.267: E/mm-camera(175): config_update_stream_info Storing stream parameters for video inst 1 as : width = 640, height 480, format = 1 inst_handle = 810081 cid = 0 
05-12 23:54:57.307: E/mm-camera(175): config_update_stream_info Storing stream parameters for video inst 3 as : width = 640, height 480, format = 1 inst_handle = 830083 cid = 0 
05-12 23:54:57.307: E/mm-camera(175): config_update_stream_info Storing stream parameters for video inst 4 as : width = 512, height 384, format = 1 inst_handle = 840084 cid = 0 
05-12 23:54:57.307: E/mm-camera(175): config_decide_vfe_outputs: Ports Used 3, Op mode 1 
05-12 23:54:57.307: E/mm-camera(175): config_decide_vfe_outputs Current mode 0 Full size streaming : Disabled 
05-12 23:54:57.307: E/mm-camera(175): config_decide_vfe_outputs: Primary: 640x480, extra_pad: 0x0, Fmt: 1, Type: 1, Path: 1 
05-12 23:54:57.307: E/mm-camera(175): config_decide_vfe_outputs: Secondary: 640x480, extra_pad: 0x0, Fmt: 1, Type: 3, Path: 4 
05-12 23:54:57.307: E/mm-camera(175): config_update_inst_handles Updated the inst handles as 810081, 830083, 0, 0 
05-12 23:54:57.387: W/ActivityManager(592): Activity pause timeout for ActivityRecord{420bd848 u0 com.brushmate.chameleon/.SettingsActivity} 
05-12 23:54:57.447: E/mm-camera(175): sensor_load_chromatix: libchromatix_imx111_zsl.so: 26 
05-12 23:54:57.537: E/mm-camera(175): camif_client_set_params: camif has associated with obj mask 0x1 
05-12 23:54:57.537: E/mm-camera(175): config_v2_CAMERA_START_common CAMIF_PARAMS_ADD_OBJ_ID failed -1 
05-12 23:54:57.537: E/mm-camera(175): vfe_operation_config: format 3 
05-12 23:54:57.537: E/mm-camera(175): vfe_operation_config:vfe_op_mode=5 
05-12 23:54:57.537: E/mm-camera(175): Invalid ASD Set Params Type 
05-12 23:54:57.537: E/mm-camera(175): vfe_set_bestshot: Bestshot mode not changed 
05-12 23:54:57.567: D/Chameleon Wallpaper(19614): Preview started 
05-12 23:54:57.607: E/mm-libcamera2(161): PROFILE HAL: First preview frame received: 1368395697.614272749 
05-12 23:54:57.607: E/BufferQueue(19614): [unnamed-19614-0] dequeueBuffer: min undequeued buffer count (2) exceeded (dequeued=6 undequeudCount=0) 
05-12 23:54:57.647: E/mm-libcamera2(161): mm_camera_dispatch_buffered_frames: mframe 0x0, sframe = 0x0 
05-12 23:54:57.647: E/mm-libcamera2(161): PROFILE HAL: stopPreview(): E: 1368395697.653949232 
05-12 23:54:57.647: E/mm-camera(175): config_MSG_ID_STOP_ACK: streamon_mask is not clear. Should not call PP_Release_HW 
05-12 23:54:57.667: D/Chameleon Wallpaper(19614): Preview stopped 
05-12 23:54:57.667: E/mm-libcamera2(161): PROFILE HAL: stopPreview(): E: 1368395697.676839512 
05-12 23:54:57.667: E/QCameraHWI(161): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*):Received Setting NULL preview window 
05-12 23:54:57.677: E/QCameraHWI(161): android::status_t android::QCameraHardwareInterface::setPreviewWindow(preview_stream_ops_t*): mPreviewWindow = 0x0x0, mStreamDisplay = 0x0x4008c378 
05-12 23:54:57.677: W/QCameraHWI_Preview(161): Setting NULL preview window 
05-12 23:54:57.677: I/CameraClient(161): Destroying camera 0 
05-12 23:54:57.687: E/mm-camera(175): config_shutdown_pp Camera not in streaming mode. Returning. 
05-12 23:54:57.687: E/mm-camera(175): vfe_ops_deinit: E 
05-12 23:54:57.758: D/Chameleon Wallpaper(19614): Camera released 
05-12 23:54:57.758: W/AudioFlinger(161): session id 77 not found for pid 161 
05-12 23:54:57.758: W/AudioFlinger(161): session id 78 not found for pid 161 
05-12 23:54:57.808: D/libEGL(19614): loaded /system/lib/egl/libEGL_adreno200.so 
05-12 23:54:57.808: D/libEGL(19614): loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
05-12 23:54:57.808: D/libEGL(19614): loaded /system/lib/egl/libGLESv2_adreno200.so 
05-12 23:54:57.818: I/Adreno200-EGL(19614): <eglInitialize:269>: EGL 1.4 QUALCOMM build: Nondeterministic AU_full_mako_PARTNER-ANDROID/JB-MR1-DEV_CL2961380_release_AU (CL2961380) 
05-12 23:54:57.818: I/Adreno200-EGL(19614): Build Date: 12/10/12 Mon 
05-12 23:54:57.818: I/Adreno200-EGL(19614): Local Branch: 
05-12 23:54:57.818: I/Adreno200-EGL(19614): Remote Branch: m/partner-android/jb-mr1-dev 
05-12 23:54:57.818: I/Adreno200-EGL(19614): Local Patches: NONE 
05-12 23:54:57.818: I/Adreno200-EGL(19614): Reconstruct Branch: NOTHING 
05-12 23:54:57.848: D/OpenGLRenderer(19614): Enabling debug mode 0 
05-12 23:54:57.888: I/ActivityManager(592): Displayed com.brushmate.chameleon/.SettingsActivity: +1s63ms 

據我瞭解,這個程序是正確的。那麼我做錯了什麼?

回答

4

經過幾天的嘗試和進一步閱讀,我發現這是一個計時問題。在調用jpeg回調之前釋放Camera對象。你只需要在回調中釋放相機。

package com.brushmate.chameleon; 

import java.io.IOException; 

import android.graphics.SurfaceTexture; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.os.SystemClock; 
import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.util.Log; 
import android.view.SurfaceView; 

public class SettingsActivity extends Activity { 
    private static final String TAG = "Chameleon Wallpaper"; 

    @SuppressLint("NewApi") protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_settings); 

     Log.d(TAG, "Activity created"); 

     Camera cam = getCamera(); 

     if (cam != null) { 
      Log.d(TAG, "Camera available"); 

      SurfaceTexture dummy = new SurfaceTexture(0); 

      try { 
      cam.setPreviewTexture(dummy); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      Log.d(TAG, "Preview texture set"); 

      cam.startPreview(); 

      Log.d(TAG, "Preview started"); 

      cam.takePicture(null, null, new Camera.PictureCallback() { 

       @Override 
       public void onPictureTaken(byte[] data, Camera camera) { 
        Log.d(TAG, "Image taken"); 

        cam.stopPreview(); 

        Log.d(TAG, "Preview stopped"); 

        cam.release(); 

        Log.d(TAG, "Camera released"); 
       } 
      }); 
     } 
    } 

    private Camera getCamera() { 
     Camera cam = null; 

     try { 
      cam = Camera.open(); 
     } catch (RuntimeException e) { 
      Log.e(TAG, "Camera not available", e); 
     } 

     return cam; 
    } 

} 
+0

感謝您分享您的解決方案 - 這也幫助我解決了*我*問題。 – 2014-04-21 22:11:33