Play with the vehicle image transmission technology, learn Opengl and Surface rendering to improve the effect of vehicle image transmission

In recent years, with the rapid development of intelligent vehicles, on-board reversing images have gradually become the standard configuration of automotive safety assistance systems, and high-definition transmission of reversing images has become the current mainstream reversing image transmission method. In this process, the application of Opengl and Surface rendering technology is also an indispensable part.

1. The principle of high-definition transmission reversing image

The high-definition transmission reversing image technology is realized based on the HDMI interface. The HDMI interface is a high-definition multimedia interface, which can transmit audio and video signals at the same time, and supports high-definition video transmission up to 1080p. In the car reversing image system, the video signal collected by the camera is transmitted to the car display screen through the HDMI interface, so as to realize the display of high-definition reversing image.

Two, Opengl rendering principle

Opengl is a cross-platform graphics rendering API that can run on a variety of hardware devices. In the car reversing image system, Opengl is mainly used for displaying the reversing image. When the camera collects the video signal, it will go through a series of processing processes such as decoding and scaling, and finally form a frame of image. Opengl implements the rendering of this frame of image through hardware acceleration, so as to achieve efficient and smooth display effect.

3. Surface rendering principle

Surface is a special view type in the Android system that allows graphics to be drawn directly on the screen. In the car reversing image system, Surface is mainly used for real-time preview of reversing images. When the vehicle is reversing, the images captured by the camera will be transmitted to the display screen through the Surface for real-time display.

4. Code implementation

The following is an example of the code implementation of Opengl rendering and Surface preview of the reversing image:

csharpCopy code// 初始化Opengl
private void initOpenGL() {
    // 创建一个OpenGL ES 2.0的上下文环境
    mEGLContext = EGL14.eglCreateContext(mEGLDisplay, mEGLConfig, EGL14.EGL_NO_CONTEXT, new int[]{EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE}, 0);
    // 创建一个渲染线程
    mRenderThread = new RenderThread();
    mRenderThread.start();
}
// Opengl渲染线程
private class RenderThread extends Thread {
    @Override
    public void run() {
        // 创建一个EGLSurface
        mEGLSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mSurfaceTexture, new int[]{EGL14.EGL_NONE}, 0);
        // 绑定EGLSurface

Five, actual combat code analysis

Below we will further illustrate the application of Opengl and Surface rendering through actual combat code.

Create SurfaceView

Define SurfaceView in the xml file:

xmlCopy code<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

Create a Renderer

Define Renderer to implement the GLSurfaceView.Renderer interface:

javaCopy codepublic class MyRenderer implements GLSurfaceView.Renderer {
    private static final String TAG = "MyRenderer";
    private int textureId;
    private SurfaceTexture surfaceTexture;
    private Surface surface;
    @Override
    public void onSurfaceCreated(GL10 gl, EGLConfig config) {
        Log.d(TAG, "onSurfaceCreated");
        // 创建纹理对象
        textureId = createTexture();
        // 创建SurfaceTexture
        surfaceTexture = new SurfaceTexture(textureId);
        // 创建Surface对象
        surface = new Surface(surfaceTexture);
    }
    @Override
    public void onSurfaceChanged(GL10 gl, int width, int height) {
        Log.d(TAG, "onSurfaceChanged");
        // 设置视口
        GLES20.glViewport(0, 0, width, height);
    }
    @Override
    public void onDrawFrame(GL10 gl) {
        Log.d(TAG, "onDrawFrame");
        // 更新纹理
        surfaceTexture.updateTexImage();
        // 清除颜色缓冲区和深度缓冲区
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
        // 使用program
        GLES20.glUseProgram(mProgram);
        // 设置顶点属性
        GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
        GLES20.glVertexAttribPointer(mTexCoordHandle, COORDS_PER_VERTEX,
                GLES20.GL_FLOAT, false, vertexStride, textureBuffer);
        GLES20.glEnableVertexAttribArray(mPositionHandle);
        GLES20.glEnableVertexAttribArray(mTexCoordHandle);
        // 绘制
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertexCount);
        // 交换缓冲区
        surface.swapBuffers();
    }
}

In the onSurfaceCreated() method, we created a texture object, created a SurfaceTexture through the texture object, and passed it to the Surface object. In the onDrawFrame() method, we update the texture, use the program, set the vertex attributes, draw a rectangle, and swap the buffers.

Initialize GLSurfaceView

Initialize the GLSurfaceView in the Activity's onCreate() method:

javaCopy codeGLSurfaceView surfaceView = findViewById(R.id.surface_view);
surfaceView.setEGLContextClientVersion(2);
surfaceView.setRenderer(new MyRenderer());

Set the OpenGL version to 2.0, and pass in the Renderer object we implemented.

Initialize Camera

We also need to initialize the Camera, and pass the preview frame data of the Camera to the Surface object we created through the Surface:

javaCopy codeprivate void initCamera() {
    try {
        Camera camera = Camera.open();
        Camera.Parameters parameters = camera.getParameters();
        // 获取最小预览尺寸
        Camera.Size size =

Summarize Opengl and Surface rendering

Opengl and Surface rendering is one of the key technologies to realize high-definition transmission of reversing images. Opengl can efficiently perform image processing and rendering through GPU hardware acceleration, so as to achieve high-quality image display; while Surface rendering is a mechanism in the Android system, which can directly render image content to the screen, and at the same time can be based on Vsync The signal achieves frame synchronization. By combining Opengl and Surface rendering technologies, smooth, high-definition reversing image transmission can be achieved, improving driving safety and user experience.

To master Opengl and Surface rendering technology, you need to have a deep understanding of Opengl graphics and Android graphics rendering mechanism, and you need to be familiar with the use of OpenGL ES and Android graphics API. In actual combat, you can gain a deep understanding of the application of Opengl and Surface rendering technologies by referring to existing open source projects, such as Google's Camera2Video example and FisheyeGL example.

For more on-vehicle development technologies, you can refer to the "Practical Handbook of Vehicle Development for Automobile Enterprises" for detailed categories, please click.

In the code implementation, you need to pay attention to the following points:

  1. Initialize the OpenGL ES context: use tools such as the EGL library and GLSurfaceView to create an OpenGL ES context and Surface object, and configure the OpenGL ES environment and parameters.
  2. Load textures and data: convert the video frames captured by the reversing camera into texture data supported by OpenGL ES, as well as other related data, such as camera preview size, frame rate, etc.
  3. Set the viewport and projection matrix: according to the screen size and image ratio, set the viewport and projection matrix of OpenGL ES to achieve the correct image display effect.
  4. Drawing graphics and textures: use the OpenGL ES drawing API and texture mapping function to draw image data to the screen.
  5. Achieve Vsync synchronization: By synchronizing with the Vsync signal, the frame rate is stable and the image is smooth.

In short, understanding the principles and implementation methods of Opengl and Surface rendering technology can help us better grasp the implementation method of high-definition transmission of reversing images, and apply them to related fields such as vehicle smart cockpits.

Guess you like

Origin blog.csdn.net/m0_62167422/article/details/130253938