0

大家好,我有以下布局:

<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

}

该程序运行良好(它拍摄了一些照片,详细说明它们并保存在 mSnapDir 目录中的输出“combined.jpg”文件中)直到几乎结束,即调用 onElaborateClicked(View view) 时。它正确地进行了详细说明并停止了预览,但是当我尝试在屏幕上显示位图时它不起作用。我添加了 SurfaceView 来调用 getHolder() (也许有一个最简单的方法)。

问题是:

1)为什么不显示位图(它存在!)?也许 SurfaceView 在 TextureView 的“背面”并且从未显示?

2) 我可以避免添加 SurfaceView 并直接在 TextureView 上完成所有工作吗?我添加 SurfaceView 只是为了显示最终的位图(我不知道其他地方该怎么做)。

任何帮助将不胜感激。提前致谢!

4

0 回答 0