2017-02-06 70 views
0

我實現了我的應用程序2種方法。首先,dispatchTakePictureIntent調用startActivityForResult。所以當我的活動完成後,我回到onActivityResult。startActivityForResult用於拍照的Android

我的應用程序使用的事件,所以當我收到一個特定的事件,我開始了攝像頭,這樣我就可以拍照。問題是:我怎麼能拍攝照片後,我開始了活動,而無需觸摸顯示屏,並且只需從遠程設備receiveing另一個事件?有沒有什麼方法可以讓我拍照而不是用手指?

感謝

private void dispatchTakePictureIntent(int actionCode) { 
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    switch(actionCode) { 
    case ACTION_TAKE_PHOTO_B: 
     File f = null; 

     try { 
      f = setUpPhotoFile(); 
      mCurrentPhotoPath = f.getAbsolutePath(); 
      takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      f = null; 
      mCurrentPhotoPath = null; 
     } 
     break; 

    default: 
     break;   
    } 
    startActivityForResult(takePictureIntent, actionCode); 
} 

,所以我想我必須作出系統調用onActivityResult ..

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    switch (requestCode) { 
    case ACTION_TAKE_PHOTO_B: { 
     if (resultCode == RESULT_OK) { 
      handleBigCameraPhoto(); 
     } 
     break; 
    } 
     default: 
      break; 
    } 
} 
+0

你可以通過實現一個自定義相機來做到這一點。上面的代碼沒有幫助它。請參閱鏈接:https://github.com/googlesamples/android-Camera2Basic –

回答

0

我實現了一個自定義的相機,通過廣播接收器接收意向。

下面的類可以用一個意圖開始,並允許拍照,保存它,切換照相機(前/後)或者通過獲取意圖找回在MainActivity。

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera_preview); 
    View myView= (View) findViewById(R.id.camera_previeww); 
    myView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); 

    cameraID= Camera.CameraInfo.CAMERA_FACING_FRONT; 
    mCamera=openCamera(cameraID); 
    mCamera.startPreview(); 

    IntentFilter filter = new IntentFilter(); 
    filter.addAction(Tabbed.BROADCAST_ACTION_TABBED); 
    LocalBroadcastManager bm = LocalBroadcastManager.getInstance(this); 
    bm.registerReceiver(mBroadcastReceiver, filter); 


    // Create our Preview view and set it as the content of our activity. 
    mPreview = new CameraPreview(this, mCamera); 
    FrameLayout preview = (FrameLayout) this.findViewById(R.id.camera_previeww); 
    preview.addView(mPreview); 
} 

private Camera.PictureCallback mPicture = new Camera.PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 

     File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE); 
     if (pictureFile == null){ 
      Log.d(TAG, "Error creating media file, check storage permissions: "); 
      return; 
     } 
     try { 
      FileOutputStream fos = new FileOutputStream(pictureFile); 
      fos.write(data); 
      fos.close(); 

     } catch (FileNotFoundException e) { 
      Log.d(TAG, "File not found: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.d(TAG, "Error accessing file: " + e.getMessage()); 
     } 
     sleep(5000); 
     camera.startPreview(); 
    } 

}; 
private static File getOutputMediaFile(int type){ 

    File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), "MyCameraApp"); 

    if (! mediaStorageDir.exists()){ 
     if (! mediaStorageDir.mkdirs()){ 
      Log.d("MyCameraApp", "failed to create directory"); 
      return null; 
     } 
    } 

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    File mediaFile; 
    if (type == MEDIA_TYPE_IMAGE){ 
     mediaFile = new File(mediaStorageDir.getPath() + File.separator + 
       "IMG_"+ timeStamp + ".jpg"); 
    }else { 
     Log.d("MyCameraApp", "mediafile=null"); 
     System.out.println("mediafile=null"); 
     return null; 
    } 

    return mediaFile; 
} 
private static Uri getOutputMediaFileUri(int type){ 
    return Uri.fromFile(getOutputMediaFile(type)); 
} 
public static final int MEDIA_TYPE_IMAGE = 1; 

@Override 
protected void onPause() { 
    super.onPause(); 
    releaseCamera();    // release the camera immediately on pause event 
} 

private void releaseCamera(){ 
    if (mCamera != null){ 
     mCamera.stopPreview(); 
     mCamera.release();  // release the camera for other applications 
     mCamera = null; 
    } 
} 


private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     byte[] data = new byte[3]; 
     if (intent !=null && intent.getAction().equals(Tabbed.BROADCAST_ACTION_TABBED)) { 
      data = intent.getByteArrayExtra(Tabbed.EXTRA_PARAM_BYTE); 
     } 
     if (data[FINGER] == MIDDLE_FINGER && data[TYPE] == SINGLE_TAP) { 
      switchCamera(); 
      //releaseCamera(); 
      //mCamera=Camera.open(); 
     } else if (data[FINGER] == MIDDLE_FINGER && data[TYPE] == DOUBLE_TAP) { 
      // HAVE TO GO BACK 
      onBackPressed(); 
      //onPause(); 
      //finish(); 
     } else if (data[FINGER] == INDEX_FINGER && data[TYPE] == SINGLE_TAP) { 
      mCamera.takePicture(null, null, mPicture); 
     } 

    // kill activity 

    } 
}; 
/* 
@Override 
public void onResume(){ 
    super.onResume(); 
    mCamera=openCamera(Camera.CameraInfo.CAMERA_FACING_FRONT); 
} 
    */ 
void kill_activity() 
{ 
    mCamera.stopPreview(); 
    mCamera.setPreviewCallback(null); 
    releaseCamera(); 
    finish(); 
} 

public void switchCamera(){ 

    mCamera.stopPreview(); 
    releaseCamera(); 
    if (cameraID==Camera.CameraInfo.CAMERA_FACING_BACK){ 
     cameraID=Camera.CameraInfo.CAMERA_FACING_FRONT; 
    }else{ 
     cameraID=Camera.CameraInfo.CAMERA_FACING_BACK; 
    } 

    mCamera=openCamera(cameraID); 
    mCamera.startPreview(); 

    CameraPreview mPreview = new CameraPreview(this, mCamera); 
    preview = (FrameLayout) this.findViewById(R.id.camera_previeww); 
    preview.removeAllViews(); 
    preview.addView(mPreview); 



} 
public Camera openCamera(int cameraIDD){ 
    Camera c=null; 
    try{ 
     c=Camera.open(cameraIDD); 
    }catch (Exception e){ 
     Log.d("Camera Activity", e.getMessage()); 
    } 
    return c; 

} 
    } 
0

做到了我的應用程序,使用此代碼:

public void takePictureNoPreview(Context context){ 
    // open back facing camera by default 
    Camera myCamera=Camera.open(); 

    if(myCamera!=null){ 
    try{ 
     //set camera parameters if you want to 
     //... 

     // here, the unused surface view and holder 
     SurfaceView dummy=new SurfaceView(context) 
     myCamera.setPreviewDisplay(dummy.getHolder());  
     myCamera.startPreview(); 

     myCamera.takePicture(null, null, getJpegCallback()): 

    }finally{ 
     myCamera.close(); 
    }  

    }else{ 
    //booo, failed! 
    } 


    private PictureCallback getJpegCallback(){ 
    PictureCallback jpeg=new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
     FileOutputStream fos; 
     try { 
      fos = new FileOutputStream("test.jpeg"); 
      fos.write(data); 
      fos.close(); 
     } catch (IOException e) { 
      //do something about it 
     } 
     } 
    }; 
    } 
} 

的代碼是從here我沒有在我的應用程序修改了很多..