大家好我有以下布局:
<SurfaceView
android:id="@+id/surf_camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<TextureView
android:id="@+id/txv_camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
android:background="#66000000" >
<Button
android:id="@+id/btn_snap"
android:layout_width="wrap_content"
android:layout_height="96dp"
android:text="Take Picture"
android:onClick="onSnapClicked"
android:textSize="36sp" />
<Button
android:id="@+id/btn_elaborate"
android:layout_width="wrap_content"
android:layout_height="96dp"
android:text="Elaborate"
android:onClick="onElaborateClicked"
android:textSize="36sp" />
使用TextureView和SurfaceView(我想我可以消除SurfaceView,但我现在还不知道如何)。
以下主要活动:
公共类MainActivity扩展了Activity 实现TextureView.SurfaceTextureListener {
private static final String TAG = "MainActivity";
// Local references to layout components
private TextureView mTxvCameraPreview;
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
// Camera manager
private CameraManager mCameraManager;
// Storage directory
private File mBaseOutDir;
private int mcounter = 0;
// Activity life-cycle -----------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Initialize layout and components
setContentView(R.layout.activity_main);
mTxvCameraPreview = (TextureView)findViewById(R.id.txv_camera_preview);
mSurfaceView = (SurfaceView) findViewById(R.id.surf_camera_preview);
// Camera managing
mCameraManager = new CameraManager(this);
mTxvCameraPreview.setSurfaceTextureListener(this);
mSurfaceHolder = mSurfaceView.getHolder();
}
@Override
protected void onResume() {
super.onResume();
if (mTxvCameraPreview.isAvailable())
mCameraManager.startPreview(mTxvCameraPreview.getSurfaceTexture());
}
@Override
protected void onPause() {
super.onPause();
mCameraManager.stopPreview();
}
// Button call-backs -------------------------------------------------------
public void onSnapClicked(View view) {
Log.i(TAG, "called: onSnapClicked");
mcounter++;
mCameraManager.takeSnapshot(mSnapDir,mcounter);
}
public void onElaborateClicked(View view) {
Log.i(TAG, "called: onElaborateClicked");
// Elaborate and save output combined picture
mCameraManager.ElaborateAndSave(mSnapDir);
// Stop the preview
mCameraManager.stopPreview();
// Lock Surface
Canvas canvas = mSurfaceHolder.lockCanvas();
// Draw bitmap
File imgFile = new File(mSnapDir, "combined.jpg");
if(imgFile.exists()){
Log.i(TAG, "file: combined.jpg found!");
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
canvas.drawBitmap(myBitmap, 0, 0, null);
mSurfaceHolder.unlockCanvasAndPost(canvas);
}
else{
Log.i(TAG, "file: combined.jpg not found!!!");
}
}
// TextureView call-backs --------------------------------------------------
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
mCameraManager.startPreview(surface);
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
mCameraManager.stopPreview();
return true;
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {} // Unused
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {} // Unused
}
该程序运行良好(需要一些镜头,精心制作并保存在输出&#34; combined.jpg&#34;目录mSnapDir中的文件)直到最后,即onElaborateClicked(View view)是调用。它正确地进行了详细说明并停止了预览,但是当我尝试在屏幕上显示位图时它不起作用。 我添加了SurfaceView来调用getHolder()(也许有一种最简单的方法)。
问题是:
1)为什么不显示位图(它存在!)?也许SurfaceView是&#34;在后面&#34; TextureView并且从未显示过?
2)我可以避免添加SurfaceView并直接在TextureView上完成所有工作?我只添加了SurfaceView来显示最终的位图(我不知道其他地方怎么做)。
任何帮助都将非常感激。 提前谢谢!