how to display yuv in android - android

In a camera preview function, I've been using yuv2rgb, and using the resulting bitmap.
This is slow, so I want to display the picture as it is.
I use example class
// public abstract class ViewBase extends SurfaceView implements
SurfaceHolder.Callback, Runnable{}

Not sure why you want to render the preview frames yourself, given that the OS already has optimized flow for the preview frame from the camera driver to the video driver.
However, if you need to do it yourself, you can use OpenGL to create a YUV texture and then blit it to a plane. Check this SO question for sample code.

Related

How to crop Camera2 preview without overlay object?

I want to crop the camera preview in Android using camera2 api. I am using android-Camera2Basic the official example.
This is the result I am getting
And, the result exactly I want to achieve is this
I don't want to overlay the object on textureView. I want it actually to be of this size without stretching.
You'll need to edit the image yourself before drawing it, since the default behavior of a TextureView is to just draw the whole image sent to its Surface.
And adjusting the TextureView's transform matrix will only scale or move the whole image, not crop it.
Doing this requires quite a bit of boilerplate, since you need to re-implement most of a TextureView. For best efficiency, you likely want to implement the cropping in OpenGL ES; so you'll need a GLSurfaceView, and then you need to use the OpenGL context of that GLSurfaceView to create a SurfaceTexture object, and then using that texture, draw a quadrilateral with the cropping behavior you want in the fragment shader.
That's fairly basic EGL, but it's quite a bit if you've never done any OpenGL programming before. There's a small test program within the Android OS tree that uses this kind of path: https://android.googlesource.com/platform/frameworks/native/+/master/opengl/tests/gl2_cameraeye/#

How to open camera with GLSurfaceView.Renderer

I'm trying to build a camera view as background.
I want to load my camera inside GLSurfaceView.Renderer class.
My class:
public class ModelRenderer implements GLSurfaceView.Renderer {
...
My question is, is it possible to load camera preview as background in GLSurfaceView.Renderer?
I'm using Android studio.
Well It isn't really adding the preview as a "background" but you certainly can draw the camera preview to the GLSurfaceView and attach a renderer to the surface to do the drawing. There are numerous examples of doing so. I would search around for them but I believe Grafika has what you are looking for.

extract Bitmap from SurfaceView [duplicate]

The difficulty I am having now is to take a screenshot of the SurfaceView. Could anyone please help?
My team is doing an Android project where we use MediaCodec to decode the video feed and render the feed onto a Surface.
The activity implements
SurfaceHolder.Callback
Then we created a SurfaceView and add the Callback:
mySurfaveView.getHolder().addCallback(this);
Then we start decode the video feed in (we have a method that does the decode)
surfaceChanged(SurfaceHolder holder, int format, int width, int height)
The video work fine, now I want to take screenshot of what gets rendered onto the SurfaceView. I have tried several ways to do this, but I didn't succeed. Following are what I have tried:
I have tired getting rootView's drawingcache.
Tried getting the frame directly from MediaCodec.
Tried the draw() method to draw on a canvas.
As of now I am tring to create a custom SurfaceView and override the onDraw method there. However, I have never created a custom SurfaceView. Am I on the right direction?
Help please. Thank you!
You can't capture a SurfaceView, as explained in this other question.
SurfaceViews will rarely want to override onDraw(). Generally the View part of the SurfaceView is a transparent hole.
For the specific case of MediaCodec output, you have a couple of choices. You can direct the output to a SurfaceTexture, and then render the video frame twice using OpenGL ES. Once to the SurfaceView for display, once to an off-screen buffer from which you can capture the pixels.
Another approach is to replace the SurfaceView with a TextureView, and then use the getBitmap() call to grab video frames.
Various examples can be found in Grafika.

faster camera preview using OpenGL?

Writing an app which takes the preview frames from camera does some transformation to it and then displays it on screen.
in
public void onPreviewFrame(byte[] data, Camera camera) {}
I take the data do yuv2rgb and some pixel manipulation in JNI in another thread. Then I create bitmap from the RGB int array and draw it using
canvas.drawBitmap(bmp, 0, 0, null);
I get around 15-20FPS on HTC Nexus One at 640x480 and 30+ FPS on Samsung Galaxy S II
I am wondering if I could speed things up by doing the drawing using Android OpenGL ES?
I would be following this guide:
http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/
Are you using SurfaceView for your camera App? The preview frame that you get is a copy of the actual frame that gets displayed on the screen. I am guessing you add another view to display the callback preview frames.
I am not a graphics guy but why don't you try SurfaceTexture. You can use setpreviewtexture() instead of setpreviewwindow(). This way Camera Service will send the buffers directly to the app, instead of making a copy and also queuing it to the AndroidNativeWindow. This might improve your performance.
This is how android Camera app Panorama works. You can find the source code here
Implemented OpenGL ES and it does help with performance

How to make change on the video stream in real time ?

I want to make some change on the video that come from the camera.
So i using class that extends the class SurfaceView and implements SurfaceHolder.Callback.
Now, i still don't find any way to make this change.
How can i do it?
you can try SurfaceTexture instead of SurfaceView, and implements interface SurfaceTexture.OnFrameAvailableListener with method onFrameAvailable(...). On arrival of video frame, Surface will call back this method, you can get the current frame data.
Pls refer to class PanoramaActivity of Android Camera APK source code for sampling code.
This is complicated and difficult to do in realtime. Basically you need to grab the camera data, modify it, then write it to the SurfaceView. And you realistically have half a second to do it, otherwise the lag is unbearable.
Apps that overlay things on a camera view (think of the ZXing barcode scanner) typically do it by providing a view bound to the camera, then grabbing a second copy of the camera image data a few times per second and overlaying additional data on top of the first view.

Categories

Resources