首页 技术 正文
技术 2022年11月7日
0 收藏 500 点赞 1,110 浏览 2735 个字

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之间的联系

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898