1

我有一个简单的 xml 布局,如下所示,FrameLayout 中只有一个 TextureView。

以及以下主要活动:

public class MainActivity   extends Activity 
implements TextureView.SurfaceTextureListener {
private static final String    TAG = "MainActivity";    

// Local references to layout components
private TextureView mTxvCameraPreview;

// Camera manager
private CameraManager mCameraManager;


@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);


    // Camera managing
    mCameraManager = new CameraManager(this);
    mTxvCameraPreview.setSurfaceTextureListener(this);
}

@Override
protected void onResume() {
    super.onResume();

    if (mTxvCameraPreview.isAvailable())
        mCameraManager.startPreview(mTxvCameraPreview.getSurfaceTexture());     
}

@Override
protected void onPause() {
    super.onPause();
    mCameraManager.stopPreview();
}


@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

}

对于每一帧,我都会做一些详细说明,没关系。我想要的是添加一个按钮来停止相机并在 TextureView 中显示从 sdcard 加载的文件。

它的伪代码可以是这样的:

public void onButtonClicked(View view) {
    // stop the surface listener (it is needed?)
    File imgFile = new  File(“/sdcard/Images/elaboration_result.jpg”);
    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
    // show myBitmap on TextureView (if possible)
}

可以在不修改 xml 布局的情况下做到这一点吗?提前致谢!

4

2 回答 2

3

恐怕您不能手动设置 TextureView 的内容。你最好的选择是在你的 TextureView 之上创建一个新的 ImageView 并在需要时设置它的图像。

public void onButtonClicked(View view) {
    // if we don't need to keep previewing new frames, stop the preview
    mCameraManager.stopPreview();
    // now, show our ImageView (which should be in front of the TextureView)
    Bitmap myBitmap = BitmapFactory.decodeFile(file_path_here);
    myImageView.setImageBitmap(myBitmap);
    myImageView.setVisibility(View.VISIBLE);
}
于 2016-02-21T21:41:24.323 回答
0

无需创建另一个Image。只需使用

textureView.getBitmap(file.getAbsolutePath);
于 2020-03-25T07:06:29.290 回答