2013-01-18 229 views
3

我有一個Activity調用另一個具有Camera Preview的類。 問題是,它打開後面的攝像頭,我需要前面。 我怎麼能做到這一點,在默認情況下,它會打開前端攝像頭(我看着谷歌,但我試過的應用程序崩潰時,每當它打開)。 下面是活動時間:Android前置攝像頭API

package com.elichai.tfillin; 

import android.app.Activity; 
import android.hardware.Camera; 
import android.os.Bundle; 
import android.widget.FrameLayout; 

public class CameraActivity extends Activity { 

    private Camera mCamera; 
    private CameraPreview mPreview; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mCamera = getCameraInstance(); 

     mPreview = new CameraPreview(this, mCamera); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
     preview.addView(mPreview); 
    } 
    public static Camera getCameraInstance(){ 
     Camera c = null; 
     try { 
      c = Camera.open(); 
     } 
     catch (Exception e){ 
     } 
     return c; 
    } 

} 這裏是其它類:

package com.elichai.tfillin; 

import java.io.IOException; 
import android.content.Context; 
import android.hardware.Camera; 
import android.hardware.Camera.CameraInfo; 
import android.util.Log; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { 
    private SurfaceHolder mHolder; 
    private Camera mCamera; 
    int cameraId=0; 

    @SuppressWarnings("deprecation") 
    public CameraPreview(Context context, Camera camera) { 
     super(context); 
     mCamera = camera; 

     mHolder = getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);   
    } 

    public void surfaceCreated(SurfaceHolder holder) { 
     try { 
      mCamera.setPreviewDisplay(holder); 
      mCamera.startPreview(); 
     } catch (IOException e) { 
     } 
    } 

    public void surfaceDestroyed(SurfaceHolder holder) { 
     mCamera.release(); 
    } 

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { 
    mCamera.setDisplayOrientation(90); 
    if (mHolder.getSurface() == null){ 
     return; 
    } 

    try { 
     mCamera.stopPreview(); 
    } catch (Exception e){ 
    } 

    try { 
     mCamera.setPreviewDisplay(mHolder); 
     mCamera.startPreview(); 

    } catch (Exception e){ 
    } 
} 

}

回答

7

您當前Camera.open()中沒有任何參數,這意味着它會打開默認攝像頭這幾乎總是面向後方的。

您應該遍歷可用的攝像頭並找出面向前方的攝像頭的ID並使用它來打開它。喜歡的東西:

private Camera openFrontFacingCamera() 
{ 
    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; 
} 

然後按如下方式使用它在你的應用程序:

public static Camera getCameraInstance() { 
    Camera c = null; 
    try { 
     c = openFrontFacingCamera(); 
    } 
    catch (Exception e){ 
    } 
    return c; 
} 

看到所有這兩種方法做的就是打開並返回相機例如,你可以很容易地通過直接簡化代碼呼叫openFrontFacingCamera()而不是getCameraInstance()

+0

謝謝!!!!,我想在屏幕中心從上到下畫相機預覽兩行,我怎麼做? – elichai2

+1

@ elichai2這與您當前的問題無關。請爲此提出一個新問題,並讓我們知道您嘗試過的內容。 –

+0

還行,但是謝謝 – elichai2

1

你可以試試這個:

private Camera openFrontFacingCameraGingerbread() 
{ 
    int Count = 0; 
    Camera cam = null; 
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
    cameraCount = Camera.getNumberOfCameras(); 
    for (int camIdx = 0; camIdx < Count; 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; 
} 

而且你還需要在menifest文件中添加權限相機。

<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

http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/或this http://stackoverflow.com/questions/7294748/how照相機頂部預覽將對您有所幫助。 – Rahil2952

+0

謝謝。我會看看那裏。 – elichai2

+0

你能舉個例子嗎?謝謝! – elichai2