我有一個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){
}
}
}
謝謝!!!!,我想在屏幕中心從上到下畫相機預覽兩行,我怎麼做? – elichai2
@ elichai2這與您當前的問題無關。請爲此提出一個新問題,並讓我們知道您嘗試過的內容。 –
還行,但是謝謝 – elichai2