2016-02-03 84 views
0

當我在應用程序中單擊捕獲照片圖標時,默認情況下它應該打開前置攝像頭,但每次都會打開相機。我需要手動更改以打開前置攝像頭。前置攝像頭未在Android平臺上打開

 So can anyone please help me out on the above issue. 

下面是我打開前置攝像頭模式的代碼。

private void getPhotoFromCamera() 
    { 
     try 
     { 
      PackageManager pm = mContext.getPackageManager(); 
      Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      intent.putExtra("android.intent.extras.CAMERA_FACING",1); 
      VisitorRegistrationFragment.this.startActivityForResult(intent,FROM_CAMERA); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     catch (OutOfMemoryError e) 
     { 
      e.printStackTrace(); 
     } 

    } 
+0

可能的重複:http://stackoverflow.com/questions/19667094/intent-does-not-set-the-camera-parameters/19670928#19670928 –

+0

我認爲[this](http://stackoverflow.com/ a/4767832/1182022)答案可能會對您有所幫助。 –

+0

可能重複的[如何在Android平臺上打開「前置攝像頭」?](http://stackoverflow.com/questions/2779002/how-to-open-front-camera-on-android-platform) –

回答

1

更改使用前置攝像頭

intent.putExtra("android.intent.extras.CAMERA_FACING",0); 

硬編碼這個值是一個糟糕的主意,應該更好地利用

intent.putExtra("android.intent.extras.CAMERA_FACING", Camera.CameraInfo.CAMERA_FACING_FRONT) 
+0

我試過了與上面的代碼也..但它不工作。以前它的工作正常,但當我從糖果升級到kitkat版本相機總是打開後面的凸輪。 –

0
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.getLocalizedMessage()); 
      } 
     } 
    } 
     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" /> 
+0

我已經在清單文件中添加了上述權限..但現在也打開回相機 –

+0

讓我再次檢查 –

+0

看到我的新評論。我測試過並且運作良好。 –

1

這是代碼來打開前置攝像頭 我javafile:

public class MainActivity extends Activity { 
    int TAKE_PHOTO_CODE = 0; 
    public static int count = 0; 

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

     // Here, we are making a folder named picFolder to store 
     // pics taken by the camera using this application. 
     final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/"; 
     File newdir = new File(dir); 
     newdir.mkdirs(); 

     Button capture = (Button) findViewById(R.id.button); 
     capture.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       // Here, the counter will be incremented each time, and the 
       // picture taken by camera will be stored as 1.jpg,2.jpg 
       // and likewise. 
       count++; 
       String file = dir + count + ".jpg"; 
       File newfile = new File(file); 
        try { 
         newfile.createNewFile(); 
        } catch (IOException e1) { 
         e1.printStackTrace(); 
        } 
       Uri outputFileUri = Uri.fromFile(newfile); 

       Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       cameraIntent.putExtra("android.intent.extras.CAMERA_FACING",1); 
       // cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); 

       startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { 
      Log.d("CameraDemo", "Pic saved"); 
     } 
    } 
} 

我的XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="aavid.rks.stackovewrflow.MainActivity"> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click pic Using Front camera" 
     android:id="@+id/button" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginLeft="93dp" 
     android:layout_marginStart="93dp" 
     android:layout_marginTop="133dp" /> 
</RelativeLayout> 

兩個Permission需要: -

<uses-permission android:name="android.permission.CAMERA"/> 

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

你測試過這個代碼? –

0

我認爲你必須添加這在你的onCreate()方法:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
    if (currentCameraId == Camera.CameraInfo.CAMERA_FACING_BACK) { 
     currentCameraId = Camera.CameraInfo.CAMERA_FACING_FRONT; 
    } else { 
     currentCameraId = Camera.CameraInfo.CAMERA_FACING_BACK; 
    } 
    camera = Camera.open(currentCameraId); 
    camera.setDisplayOrientation(90); 
    try { 
     camera.setPreviewDisplay(surfaceHolder); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    camera.startPreview(); 

它爲我工作。希望對你也有幫助。