Вопрос

I have a simple xml layout as follow with just a TextureView inside a FrameLayout.

And the following main activity:

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

}

For every frame I do some elaboration and it is OK. What I want is to add a button which stops the camera and display in the TextureView a file load from sdcard.

A pseudo-code of this can be something like this:

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)
}

It is possible to do this without modifying the xml layout? Thanks in advance!

Это было полезно?

Решение

You cannot set the content of a TextureView manually, I'm afraid. Your best bet is to make a new ImageView on top of your TextureView and set its image when you need to.

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

Другие советы

No need to create another Image. Just use

textureView.getBitmap(file.getAbsolutePath);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top