您好我想在我的Android手机上并排显示实时相机预览。 像这样:
https://lh6.googleusercontent.com/-Ngo4tD4AvWY/U8JOHxxQ9KI/AAAAAAAACK8/NZjnw1Yqg8A/w384-h287/2.jpg
只要左半屏显示实时预览或右侧显示, 但如果我想同时显示左侧和右侧,我只是得到一个黑屏 我不知道为什么。
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.test.normaltest.sbscameraview" >
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity
public class MainActivity extends Activity {
private CameraView cameraView1;
private CameraView cameraView2;
private Camera mCamera1;
private Camera mCamera2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCamera1 = getCameraInstance();
cameraView1 = new CameraView(this, mCamera1);
cameraView2 = new CameraView(this, mCamera1);
FrameLayout preview1 = (FrameLayout) findViewById(R.id.camera_preview1);
preview1.addView(cameraView1);
FrameLayout preview2 = (FrameLayout) findViewById(R.id.camera_preview2);
preview2.addView(cameraView2);
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
}
catch (Exception e){
Log.i("camera", "null");
}
return c; // returns null if camera is unavailable
}
}
CameraView
public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraView(Context context, Camera camera) {
super(context);
mHolder = getHolder();
mHolder.addCallback(this);
mCamera = camera;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
Log.i("CameraView", "surfaceChanged");
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}