2016-09-10 63 views
4

我想製作一個應用程序,只能打開前置攝像頭,如何使用intent如何在Android中使用Intent打開前置攝像頭?

private void captureImage() { 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 
    intent.putExtra("android.intent.extras.CAMERA_FACING", 1); 

    // start the image capture Intent 
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); 
} 
+0

你會發布開放前置攝像頭的完整解決方案嗎? –

回答

2
try this: 

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
intent.putExtra("android.intent.extras.CAMERA_FACING", 1); 
       File outPutFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + Util.SD_CARD_PATH); 
       if (!outPutFile.exists()) { 
        outPutFile.mkdirs(); 
       } 
       capturedImageUri = Uri.fromFile(File.createTempFile("packagename" + System.currentTimeMillis(), ".jpg", outPutFile)); 

       intent.putExtra(MediaStore.EXTRA_OUTPUT, capturedImageUri); 
       startActivityForResult(intent, Util.REQUEST_CAMERA); 
+0

什麼是capturedImageUri? –

+0

它和你的文件一樣。它會將點擊的圖像存儲在該uri –

+0

ok.But Util不解決。 –

10

的Java

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
    Uri photoUri = Uri.fromFile(getOutputPhotoFile()); 
    intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); 
    intent.putExtra("android.intent.extras.CAMERA_FACING", 1); 
    startActivityForResult(intent, CAMERA_PHOTO_REQUEST_CODE); 

其他/備用解決方案

private Camera openFrontFacingCameraGingerbread() { 
    int cameraCount = 0; 
    Camera cam = null; 
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
    cameraCount = Camera.getNumberOfCameras(); 
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) { 
     Camera.getCameraInfo(camIdx, cameraInfo); 
     if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
      try { 
       cam = Camera.open(camIdx); 
      } catch (RuntimeException e) { 
       Log.e(TAG, "Camera failed to open: " + e.toString()); 
      } 
     } 
    } 

    return cam; 
} 

AndroidManifest.xml文件中添加這些權限

<uses-permission android:name="android.permission.CAMERA" /> 
<uses-feature android:name="android.hardware.camera" android:required="false" /> 
<uses-feature android:name="android.hardware.camera.front" android:required="false" /> 

僅限薑餅(2.3)和Android版本。

否則,你也可以檢查這些例子

1. android-Camera2Basic

2. Camera Example 2

3. Vogella example

希望它可以幫助你..

+1

我用相機API來打開前置攝像頭,但是在捕獲圖像後,它在90度時鐘轉動後保存圖像。我如何正確保存圖像。 –

+0

可能是這個[**答案**](http://stackoverflow.com/a/12933632/6617272)會幫助你 – skydroid

0

標準Android相機

使用Intent您打開標準的安卓相機應用程序。

切勿使用android.intent.extras.CAMERA_FACING屬性 - 這是一個無證的功能,停止工作從一些安卓版本的開始。

相機API

要打開一個前置攝像頭,你應該使用攝像機API - 不喜歡的東西選擇了前置攝像頭,顯示在視圖中的預覽,並手動拍照。 @skydroid的答案顯示瞭如何找到前置攝像頭。請注意,Camera.open()未按照您的預期爲用戶打開相機,您應該手動顯示預覽。

還請注意,由於API級別21 Camera API已被棄用,文檔建議使用Camera 2 API來代替。但Camera API保持完整功能,如果您希望支持舊版本(< API級別21),則別無選擇。

相關問題