2013-04-24 63 views
0

我有這個活動(MakePhotoActivity)類,當您打開應用程序時需要拍照。我已經設置它使用前置攝像頭。在短信上用相機拍攝照片

然後我有另一個班,這是一個SMS廣播接收器已經工作。

現在,我想使它在接收短信時,我希望它能夠使用當前的課程拍攝照片。但我怎樣才能將它們整合在一起?

正如我已經嘗試複製方法(surfaceChanged等)到我的廣播接收機類,並在短信接收,我放置在onCreate(在MakePhotoActivity)內的代碼。它不工作。

public class MakePhotoActivity extends Activity implements SurfaceHolder.Callback 
{ 
    //a variable to store a reference to the Image View at the main.xml file 
    private ImageView iv_image; 
    //a variable to store a reference to the Surface View at the main.xml file 
    private SurfaceView sv; 

    //a bitmap to display the captured image 
    private Bitmap bmp; 
    private int cameraId = 0; 

    //Camera variables 
    //a surface holder 
    private SurfaceHolder sHolder; 
    //a variable to control the camera 
    private Camera mCamera; 
    //the camera parameters 
    private Parameters parameters; 


    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     //get the Image View at the main.xml file 
     iv_image = (ImageView) findViewById(R.id.imageView); 

     //get the Surface View at the main.xml file 
     sv = (SurfaceView) findViewById(R.id.surfaceView); 

     //Get a surface 
     sHolder = sv.getHolder(); 

     //add the callback interface methods defined below as the Surface View callbacks 
     sHolder.addCallback(this); 

     //tells Android that this surface will have its data constantly replaced 
     sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    private int findFrontFacingCamera() { 
     int cameraId = -1; 
     // Search for the front facing camera 
     int numberOfCameras = Camera.getNumberOfCameras(); 
     for (int i = 0; i < numberOfCameras; i++) { 
      CameraInfo info = new CameraInfo(); 
      Camera.getCameraInfo(i, info); 
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
       Log.d("A", "Camera found"); 

       cameraId = i; 
       break; 
      } 
     } 
     return cameraId; 
    } 

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) 
    { 
     //get camera parameters 
     parameters = mCamera.getParameters(); 

     //set camera parameters 
     mCamera.setParameters(parameters); 
     mCamera.startPreview(); 

     //sets what code should be executed after the picture is taken 
     Camera.PictureCallback mCall = new Camera.PictureCallback() 
     { 

      public void onPictureTaken(byte[] data, Camera camera) 
      { 
       //decode the data obtained by the camera into a Bitmap 
       bmp = BitmapFactory.decodeByteArray(data, 0, data.length); 
       //set the iv_image 
       iv_image.setImageBitmap(bmp); 
       FileOutputStream outStream = null; 
       try{ 
        outStream = new FileOutputStream("/sdcard/Image"+System.currentTimeMillis()+".jpg"); 
        outStream.write(data); 
        outStream.close(); 
       } catch (FileNotFoundException e){ 
        Log.d("CAMERA", e.getMessage()); 
       } catch (IOException e){ 
        Log.d("CAMERA", e.getMessage()); 
       } 
      } 
     }; 

     mCamera.takePicture(null, null, mCall); 
    } 


    public void surfaceCreated(SurfaceHolder holder) 
    { 
     // The Surface has been created, acquire the camera and tell it where 
     // to draw the preview. 

     if (mCamera == null) { 
      cameraId = findFrontFacingCamera(); 
      mCamera = Camera.open(cameraId); 
      try { 
       mCamera.setPreviewDisplay(holder); 

       // TODO test how much setPreviewCallbackWithBuffer is faster 
       // mCamera.setPreviewCallback((PreviewCallback) this); 
      } catch (IOException e) { 
       mCamera.release(); 
       mCamera = null; 
      } 
     } 
    } 


    public void surfaceDestroyed(SurfaceHolder holder) 
    { 
     if (mCamera != null) { 
      mCamera.stopPreview(); 
      mCamera.setPreviewCallback(null); 
      mCamera.release(); 
      mCamera = null; 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 
     if (mCamera!=null) 
     { 
      mCamera.stopPreview(); 
      mCamera.release(); 
      mCamera=null; 
     } 
    } 
} 

更新:這是我所做的。投入服務,但我得到的是說應用程序通過零位面的誤差,攝像機服務器死了!死ICamera,錯誤100

我引用來自http://easyandroidtutorials.blogspot.in/2012/09/capture-image-without-preview-as.html的代碼,並做了一些細微的變化,仍然無法正常工作。

public class CameraService extends Service 
{ 
    //Camera variables 
    //a surface holder 
    private SurfaceHolder sHolder; 
    //a variable to control the camera 
    private Camera mCamera; 
    //the camera parameters 
    private Parameters parameters; 
    SurfaceView sv; 
    private int cameraId = 0; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     Log.i("Service", "Service started"); 




    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     try { 


       if (mCamera == null) { 
        cameraId = findFrontFacingCamera(); 
        mCamera = Camera.open(cameraId); 

       try { 
        // Thread.sleep(3000); 
        sv = new SurfaceView(getApplicationContext()); 
        mCamera.setPreviewDisplay(sv.getHolder()); 
        parameters = mCamera.getParameters(); 

        //set camera parameters 
        mCamera.setParameters(parameters); 
        mCamera.startPreview(); 
        mCamera.takePicture(null, null, mCall); 

       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        mCamera.release(); 
        mCamera = null; 
        e.printStackTrace(); 
       } 


       //Get a surface 
       //sHolder = sv.getHolder(); 
       //tells Android that this surface will have its data constantly replaced 
       // sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return Service.START_STICKY; 
    } 

    private int findFrontFacingCamera() { 
     int cameraId = -1; 
     // Search for the front facing camera 
     int numberOfCameras = Camera.getNumberOfCameras(); 
     for (int i = 0; i < numberOfCameras; i++) { 
      CameraInfo info = new CameraInfo(); 
      Camera.getCameraInfo(i, info); 
      if (info.facing == CameraInfo.CAMERA_FACING_FRONT) { 
       Log.d("A", "Camera found"); 

       cameraId = i; 
       break; 
      } 
     } 
     return cameraId; 
    } 

    Camera.PictureCallback mCall = new Camera.PictureCallback() 
    { 

     public void onPictureTaken(byte[] data, Camera camera) 
     { 
      //decode the data obtained by the camera into a Bitmap 

      FileOutputStream outStream = null; 
      try{ 
       outStream = new FileOutputStream("/sdcard/Image.jpg"); 
       outStream.write(data); 
       outStream.close(); 
      } catch (FileNotFoundException e){ 
       Log.d("CAMERA", e.getMessage()); 
      } catch (IOException e){ 
       Log.d("CAMERA", e.getMessage()); 
      } 

     } 
    }; 


    @Override 
    public IBinder onBind(Intent intent) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

任何人有任何幫助嗎?謝謝。在開始的活動

採取在服務畫面將要求您創建一個虛擬的更多信息

Intent intent = new Intent(this, MakePhotoActivity.class); 
startActivity(intent); 

檢查http://developer.android.com/training/basics/firstapp/starting-activity.html

+0

你讀過[BroadcastReceiver文檔](http://developer.android.com/reference/android/content/BroadcastReceiver.html)和[Intent and intent filters](http://developer.android。 com/guide/components/intents-filters.html)都來自Android官方文檔? – 2013-04-24 18:38:25

+0

嗨,我不是試圖開始短信接收的活動。相反,我試圖讓它在沒有活動類的情況下自動接收短信。 – user1778855 2013-04-24 18:40:31

+0

對不起,現在我明白了。 現在,如果我沒有錯,理論上說你應該運行[service](http://developer.android.com/guide/components/services.html)來做到這一點,但我不知道爲什麼你的代碼失敗了。 正在拋出哪個錯誤? – 2013-04-24 18:53:02

回答

0
在您的SMS廣播reciever的的onReceive方法

做到這一點surfaceview。下面是應解釋如何做到這一點的鏈接: how to take camera capture without a preview from a service or thread?

,如果你想關閉快門聲音: camera.enableShutterSound(假); http://developer.android.com/reference/android/hardware/Camera.html#enableShutterSound(boolean)

+0

嗨,我可能忘記提及活動課不應該開放。這意味着照片將會在沒有任何互動的情況下進行,就像短信收到時一樣,它會拍攝照片。我可以利用廣播接收機內現有的活動代碼和地點嗎? – user1778855 2013-04-24 18:30:16

+0

我從來沒有從SMSBroadcastReciever捕獲圖像,但它應該工作。如果它沒有,請嘗試創建一個服務或一個可以進行圖像捕獲的交易活動 – 2013-04-24 18:49:59

+0

我已經嘗試創建一個虛擬表面視圖,但它不起作用,給了我一些錯誤。看到我更新的帖子。 – user1778855 2013-04-24 18:59:38

相關問題