11

I have this android application.

It use a SurfaceView, from where I get the Surface through the SurfaceHolder.

It also use ExoPlayer to stream videos. However I have instantiated an ImageReader, getting its Surface and passing to the ExoPlayer.

Now, I am in the ImageReader.OnImageAvailableListener#onImageAvailable and I access the latest Image.

I want to manipulate the Image and send the new data to the "SurfaceView" Surface.

How can I "draw" an android.media.Image to an android.view.Surface ?

Vito De Tullio
  • 2,505
  • 3
  • 32
  • 47
  • There is code floating around for converting an `Image` obtained from a `MediaProjection` (Android 5.0+ screenshot API) into a `Bitmap`, taking stride into account. Those have only one plane, though. I assume that there is a recipe for doing the same sort of thing for a YUV `Image`. – CommonsWare Sep 04 '15 at 22:06
  • Were you able to do this ? – RohitMat Feb 20 '18 at 08:58

3 Answers3

0

The question is not clear to me. The way to get android.media.Image is by the Camera2 API, and there you can provide a surface and the "camera" will draw over it. Please refer to Camera2Video example

Another way to get the Image object is from ImageReader (while decoding video for example). In this case you want to draw the image, but you can not provide a surface to the ImageReader(there is an internal surface that is not displayed). In this case you can draw the Image on a SurfaceView. Assuming this is the case, you need to convert an Image to a Bitmap objects.

You have discussion about how perform this here

Arkady
  • 574
  • 1
  • 7
  • 17
-1

Possible duplicate of: how to draw image on surfaceview android

First get your canvas by using lockCanvas() (see here), second get your image and make it a drawable using:

my_bitmap = Bitmap.createBitmap(
  MediaStore.Images.Media.getBitmap(getContentResolver(), uri),
  0,0,90, 90);
drawable=new BitmapDrawable(my_bitmap); 

After that you can draw the drawable to the locked canvas and use unlockCanvasAndPost (Canvas canvas) to post the updated canvas back to the surfaceview.

Community
  • 1
  • 1
Bas van Stein
  • 10,298
  • 3
  • 25
  • 62
-4

here is the answer for your question.

MainActivity.java

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mySurfaceView mySurfaceView = new mySurfaceView(getApplicationContext());
        setContentView(mySurfaceView);
    }
}

mySurfaceView.java

public class mySurfaceView extends SurfaceView implements
    SurfaceHolder.Callback {

private TutorialThread _thread;

public mySurfaceView(Context context) {
    super(context);
    getHolder().addCallback(this);
    _thread = new TutorialThread(getHolder(), this);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Bitmap _scratch = BitmapFactory.decodeResource(getResources(),
            R.drawable.icon);
    canvas.drawColor(Color.BLACK);
    canvas.drawBitmap(_scratch, 10, 10, null);

}

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}

public void surfaceCreated(SurfaceHolder arg0) {
    _thread.setRunning(true);
    _thread.start();
}

public void surfaceDestroyed(SurfaceHolder arg0) {
    boolean retry = true;
    _thread.setRunning(false);
    while (retry) {
        try {
            _thread.join();
            retry = false;
        } catch (InterruptedException e) {
        }
    }
}

class TutorialThread extends Thread {
    private SurfaceHolder _surfaceHolder;
    private mySurfaceView _panel;
    private boolean _run = false;

    public TutorialThread(SurfaceHolder surfaceHolder, mySurfaceView panel) {
        _surfaceHolder = surfaceHolder;
        _panel = panel;
    }

    public void setRunning(boolean run) {
        _run = run;
    }

    @Override
    public void run() {
        Canvas c;
        while (_run) {
            c = null;
            try {
                c = _surfaceHolder.lockCanvas(null);
                synchronized (_surfaceHolder) {
                    _panel.onDraw(c);
                }
            } finally {
                if (c != null) {
                    _surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}
}
Parth Bhayani
  • 2,176
  • 3
  • 15
  • 35