zoukankan      html  css  js  c++  java
  • 使用SurfaceView播放RGB原始视频-2016.01.22

    1 程序代码

    使用Android中的SurfaceView播放RGB视频数据,SufaceView播放代码如下:

    package com.zhoulee.surfaceviewdemo;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Rect;
    import android.util.Log;
    import android.view.SurfaceHolder;
    import android.view.SurfaceView;
    
    public class MySurfaceView extends SurfaceView implements
            SurfaceHolder.Callback
    {
        private static String TAG = "MySurfaceView";
        private static String RGB_FILE_NAME = "/data/video/bxjg_352x288.rgba";
        private static int PICTURE_WIDTH = 352;
        private static int PICTURE_HEIGHT = 288;
        private  static int PICTURE_SIZE = PICTURE_WIDTH * PICTURE_HEIGHT * 4;
        private  Rect m_srcRect;
        private  Rect m_dstRect;
        private SurfaceHolder m_surfaceHolder;
        private boolean m_flag;
        private Canvas m_canvas;
        private FileInputStream m_fileInputStream;
        
        byte [] m_pixel = new byte[PICTURE_SIZE];
        
        private Thread m_thread = new Thread(new Runnable()
        {
            @Override
            public void run() {
                while(m_flag)
                {
                    m_canvas = m_surfaceHolder.lockCanvas();                
                    m_canvas.drawColor(Color.BLACK);
                    
                    try {
                        if(-1 == m_fileInputStream.read(m_pixel))
                        {
                            break;
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    
                    ByteBuffer buffer = ByteBuffer.wrap(m_pixel);
                    
                    Bitmap videoBitmap = Bitmap.createBitmap(PICTURE_WIDTH, PICTURE_HEIGHT, Config.ARGB_8888);
                    
                    videoBitmap.copyPixelsFromBuffer(buffer);
                    
                    m_canvas.drawBitmap(videoBitmap, m_srcRect, m_dstRect, null);
                                 
                    if(m_canvas != null)
                    {
                        m_surfaceHolder.unlockCanvasAndPost(m_canvas);
                    }
                    
                    try
                    {
                        Thread.sleep(5);
                    }
                    catch(InterruptedException e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        });
    
        public MySurfaceView(Context context) {
            super(context);
            Log.i(TAG, "MySurfaceView Constructor");
            m_flag = false;
            m_surfaceHolder = this.getHolder();
            m_surfaceHolder.addCallback(this);
            m_srcRect = new Rect(0, 0, PICTURE_WIDTH, PICTURE_HEIGHT);
            m_dstRect = new Rect(800, 50, 800 + PICTURE_WIDTH, 50 + PICTURE_HEIGHT);
        }
    
        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            Log.i(TAG, "surfaceCtreated");
            m_flag = true;
            try {
                m_fileInputStream = new FileInputStream(RGB_FILE_NAME);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            m_thread.start();
        }
    
        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            Log.i(TAG, "surfaceChanged");
        }
    
        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            Log.i(TAG, "surfaceDestroyed");
            m_flag = false;
        }
    }

    入口Activity代码如下:

    package com.zhoulee.surfaceviewdemo;
    
    import android.app.Activity;
    import android.os.Bundle;
    
    public class SurfaceViewDemoActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(new MySurfaceView(this));
        }
    }

    2 参考资料

    How to load RGB565 buffer to ImageView

    android显示RGB565数据图像

    Android图形系统之Surface、SurfaceView、SurfaceHolder及SurfaceHolder.Callback之间的联系

  • 相关阅读:
    Oracle 建用户、 表空间脚本
    Java常见Jar包的用途
    EF:无法检查模型兼容性,因为数据库不包含模型元数据。
    Eclipse -Xms256M -Xmx640M -XX:PermSize=256m -XX:MaxPermSize=768m
    CentOS远程连接Windows操作系统
    spring boot / cloud (二十) 相同服务,发布不同版本,支撑并行的业务需求
    jvm
    jvm
    spring boot / cloud (十九) 并发消费消息,如何保证入库的数据是最新的?
    spring boot / cloud (十八) 使用docker快速搭建本地环境
  • 原文地址:https://www.cnblogs.com/zhouLee/p/5150839.html
Copyright © 2011-2022 走看看