2013-06-01 17 views
1

之間我要添加一個按鈕來正面和背面相機的Android - 切換正面和背面相機

mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera 

之間切換如上文代碼段I所示可以通過簡單地添加+1改變相機。

例子:mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID + 1)

但我想通過一個按鈕來做到這一點。我怎樣才能實現這個

謝謝

private static final String TAG = "Sample::SurfaceView"; 
private SurfaceHolder  mHolder; 
private VideoCapture  mCamera; 
private FpsMeter   mFps; 

public SampleCvViewBase(Context context) { 
    super(context); 
    mHolder = getHolder(); 
    mHolder.addCallback(this); 
    mFps = new FpsMeter(); 
    Log.i(TAG, "Instantiated new " + this.getClass()); 
} 

public boolean openCamera() { 
    Log.i(TAG, "openCamera"); 
    synchronized (this) { 
     releaseCamera(); 
     mCamera = new VideoCapture(Highgui.CV_CAP_ANDROID);//add + 1 to use front camera 
     if (!mCamera.isOpened()) { 
      mCamera.release(); 
      mCamera = null; 
      Log.e(TAG, "Failed to open native camera"); 
      return false; 
     } 
    } 
    return true; 
} 

public void releaseCamera() { 
    Log.i(TAG, "releaseCamera"); 
    synchronized (this) { 
     if (mCamera != null) { 
       mCamera.release(); 
       mCamera = null; 
     } 
    } 
} 

public void setupCamera(int width, int height) { 
    Log.i(TAG, "setupCamera("+width+", "+height+")"); 
    synchronized (this) { 
     if (mCamera != null && mCamera.isOpened()) { 
      List<Size> sizes = mCamera.getSupportedPreviewSizes(); 
      int mFrameWidth = width; 
      int mFrameHeight = height; 

      // selecting optimal camera preview size 
      { 
       double minDiff = Double.MAX_VALUE; 
       for (Size size : sizes) { 
        if (Math.abs(size.height - height) < minDiff) { 
         mFrameWidth = (int) size.width; 
         mFrameHeight = (int) size.height; 
         minDiff = Math.abs(size.height - height); 
        } 
       } 
      } 

      mCamera.set(Highgui.CV_CAP_PROP_FRAME_WIDTH, mFrameWidth); 
      mCamera.set(Highgui.CV_CAP_PROP_FRAME_HEIGHT, mFrameHeight); 
     } 
    } 

} 

public void surfaceChanged(SurfaceHolder _holder, int format, int width, int height) { 
    Log.i(TAG, "surfaceChanged"); 
    setupCamera(width, height); 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    Log.i(TAG, "surfaceCreated"); 
    (new Thread(this)).start(); 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    Log.i(TAG, "surfaceDestroyed"); 
    releaseCamera(); 
} 

protected abstract Bitmap processFrame(VideoCapture capture); 

public void run() { 
    Log.i(TAG, "Starting processing thread"); 
    mFps.init(); 

    while (true) { 
     Bitmap bmp = null; 

     synchronized (this) { 
      if (mCamera == null) 
       break; 

      if (!mCamera.grab()) { 
       Log.e(TAG, "mCamera.grab() failed"); 
       break; 
      } 

      bmp = processFrame(mCamera); 

      mFps.measure(); 
     } 

     if (bmp != null) { 
      Canvas canvas = mHolder.lockCanvas(); 
      if (canvas != null) { 
       canvas.drawBitmap(bmp, (canvas.getWidth() - bmp.getWidth())/2, (canvas.getHeight() - bmp.getHeight())/2, null); 
       // mFps.draw(canvas, (canvas.getWidth() - bmp.getWidth())/2, 0); 
       mHolder.unlockCanvasAndPost(canvas); 
      } 
      bmp.recycle(); 
     } 
    } 

    Log.i(TAG, "Finishing processing thread"); 
} 

}

回答

3

我覺得這link對你有幫助的正面和背面攝像頭之間切換。

檢查another問題的解決方案。希望這對你有所幫助。

+0

感謝您的鏈接是有幫助的..但它沒有成功..如果有人可以給我一些代碼相關的代碼我發佈將非常有幫助。謝謝 :) – PDS