android 采集摄像头帧图像数据并显示
来源:互联网 发布:手机淘宝在哪里看等级 编辑:程序博客网 时间:2024/05/16 12:56
package com.example.ict.video_frame;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Matrix;import android.graphics.PixelFormat;import android.hardware.Camera;import android.media.Image;import android.os.Bundle;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.widget.Button;import android.widget.ImageView;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;public class MainActivity extends Activity{ private Camera mCamera = null; private SurfaceHolder holder = null; int filename = 0; private Button button1,button2; private ImageView imageView = null; private int width = 640; private int height = 480; private Bitmap VideoBit = null; private ByteBuffer byteBuffer = null; private byte rgb_data[] = new byte[width*height*4]; class Callback implements Camera.PreviewCallback { @Override public void onPreviewFrame(byte[] frame, Camera camera) { // writeFileSDcardFile_byte("/mnt/sdcard/DCIM/"+String.valueOf(filename)+".txt",frame); decodeYUV420SP(rgb_data,frame,width,height); VideoBit = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888); byteBuffer = ByteBuffer.wrap(rgb_data); byteBuffer.position(0); VideoBit.copyPixelsFromBuffer(byteBuffer); Matrix matrix = new Matrix(); matrix.postRotate(90); imageView.setImageBitmap(Bitmap.createBitmap(VideoBit, 0, 0, width, height, matrix, true)); filename++; } } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); SurfaceView mSurfaceView = (SurfaceView) this.findViewById(R.id.camera_preview); button1 = (Button) findViewById(R.id.b1); button2 = (Button) findViewById(R.id.b2); imageView = (ImageView)findViewById(R.id.imageView); holder = mSurfaceView.getHolder(); button1.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { if(mCamera == null) { mCamera = Camera.open(); Camera.Parameters p = mCamera.getParameters(); p.setPreviewFormat(PixelFormat.YCbCr_420_SP); p.setPreviewSize(width,height); p.setPreviewFrameRate(15); //设置帧率 mCamera.setParameters(p); mCamera.setDisplayOrientation(90); try { mCamera.setPreviewDisplay(holder); } catch (IOException e) { e.printStackTrace(); } mCamera.startPreview(); Callback a = new Callback(); mCamera.setPreviewCallback(a); } } }); button2.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View v) { finish(); } }); holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } @Override public void finalize() { try { super.finalize(); } catch (Throwable e) { e.printStackTrace(); } } public void writeFileSDcardFile(String fileName,String write_str) throws IOException{ try{ FileOutputStream fout = new FileOutputStream(fileName); byte []bytes = write_str.getBytes(); fout.write(bytes); fout.close(); }catch (Exception e) { e.printStackTrace(); } } public void writeFileSDcardFile_byte(String fileName,byte bytes[]) throws IOException{ try{ FileOutputStream fout = new FileOutputStream(fileName); fout.write(bytes); fout.close(); }catch (Exception e) { e.printStackTrace(); } } static public void decodeYUV420SP(byte[] rgb, byte[] yuv420sp, int width, int height) { final int frameSize = width * height; for (int j = 0, yp = 0; j < height; j++) { int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; for (int i = 0; i < width; i++, yp++) { int y = (0xff & ((int) yuv420sp[yp])) - 16; if (y < 0) y = 0; if ((i & 1) == 0) { v = (0xff & yuv420sp[uvp++]) - 128; u = (0xff & yuv420sp[uvp++]) - 128; } int y1192 = 1192 * y; int r = (y1192 + 1634 * v); int g = (y1192 - 833 * v - 400 * u); int b = (y1192 + 2066 * u); if (r < 0) r = 0; else if (r > 262143) r = 262143; if (g < 0) g = 0; else if (g > 262143) g = 262143; if (b < 0) b = 0; else if (b > 262143) b = 262143; rgb[yp*4] = (byte)(r >>10); rgb[yp*4+1] = (byte)(g >>10); rgb[yp*4+2] = (byte)(b >> 10); rgb[yp*4+3] = (byte)255; } } }}
0 0
- android 采集摄像头帧图像数据并显示
- OpenCV 获取摄像头图像数据并显示
- android环境下摄像头数据采集及显示
- openCV2.4.9与QT5.5.1结合打开摄像头并显示采集到的图像的问题
- opencv采集视频/摄像头数据并保存
- 读摄像头数据并显示
- 读摄像头数据并显示
- 调用摄像头采集图像
- 双目摄像头采集图像
- Qt环境下利用OpenCV采集摄像头图像并保存
- Opencv2打开摄像头并显示图像
- opencv实现摄像头的实时图像采集与显示
- Linux下USB摄像头采集图像,液晶framebuffer显示
- 摄像头采集,framebuffer显示
- V4L2采集摄像头数据
- V4L2摄像头采集数据
- V4L2摄像头采集数据
- V4L2摄像头采集数据
- sass and compass
- git on server(ssh)深入剖析,github提供服务原理
- Mysql索引
- Linux IO复用区别与epoll详解
- 多达288种动态效果的侧滑菜单开源库,满足您项目的各种需求
- android 采集摄像头帧图像数据并显示
- python pprint模块
- 【HTML】HTML基础
- Android Patch 方案与持续交付
- STS指定IDE使用JDK
- 在Eclipse中编写servlet时出现"The import javax.servlet cannot be resolved" 问题解决办法
- zzulioj 1902 (985的因子对难题)
- opengl es坐标变换2
- C++的静态绑定