1

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!

Carl0s1z
  • 4,683
  • 7
  • 32
  • 47
user3318590
  • 81
  • 1
  • 12
  • You should be able to disconnect the camera from the TextureView by stopping the camera preview, then just lock the Surface to get a Canvas and draw your bitmap. You probably can't go back -- once you connect a Canvas to a Surface it doesn't like to let go -- but if you're about to close the Activity anyway that won't matter. – fadden Apr 10 '14 at 14:51
  • OK thanks, but how I can do this? Are somebody able to complete the pseudo-code I wrote? Thanks again! – user3318590 Apr 11 '14 at 05:41

2 Answers2

3

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);
}
Community
  • 1
  • 1
VinceFior
  • 1,279
  • 13
  • 25
1

No need to create another Image. Just use

textureView.getBitmap(file.getAbsolutePath);
mohammed wazeem
  • 1,310
  • 1
  • 10
  • 26
Zerocool
  • 29
  • 2