android camera 视频采集,android camera 通过onPreviewFrame采集视频

package com.example.camera;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

import android.app.Activity;

import android.graphics.Rect;

import android.graphics.YuvImage;

import android.hardware.Camera;

import android.hardware.Camera.Size;

import android.os.Bundle;

import android.os.Environment;

import android.view.SurfaceHolder;

import android.view.SurfaceView;

public class MainActivity extends Activity implements SurfaceHolder.Callback,

Camera.PreviewCallback {

private SurfaceView mSurfaceview = null; // SurfaceView对象:(视图组件)视频显示

private SurfaceHolder mSurfaceHolder = null; // SurfaceHolder对象:(抽象接口)SurfaceView支持类

private Camera mCamera = null; // Camera对象,相机预览

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mSurfaceview = (SurfaceView) findViewById(R.id.surfaceview);

mSurfaceHolder = mSurfaceview.getHolder(); // 绑定SurfaceView,取得SurfaceHolder对象

mSurfaceHolder.addCallback(this); // SurfaceHolder加入回调接口

// mSurfaceHolder.setFixedSize(176, 144); // 预览大小設置

mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 設置顯示器類型,setType必须设置

}

@Override

public void surfaceChanged(SurfaceHolder holder, int format, int width,

int height) {

if (mCamera == null) {

return;

}

mCamera.stopPreview();

mCamera.setPreviewCallback(this);

mCamera.startPreview();

}

@Override

protected void onResume() {

super.onResume();

mCamera = Camera.open();

}

@Override

protected void onPause() {

super.onPause();

if (mCamera != null) {

mCamera.release();

mCamera = null;

}

}

@Override

public void surfaceCreated(SurfaceHolder holder) {

try {

if (mCamera != null) {

mCamera.setPreviewDisplay(mSurfaceHolder);

mCamera.startPreview();

}

} catch (IOException e) {

e.printStackTrace();

}

}

@Override

public void surfaceDestroyed(SurfaceHolder holder) {

}

@Override

public void onPreviewFrame(byte[] data, Camera camera) {

// 刚刚拍照的文件名

String fileName = "IMG_"

+ new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date())

.toString() + ".jpg";

File sdRoot = Environment.getExternalStorageDirectory();

String dir = "/Camera/";

File mkDir = new File(sdRoot, dir);

if (!mkDir.exists())

mkDir.mkdirs();

File pictureFile = new File(sdRoot, dir + fileName);

if (!pictureFile.exists()) {

try {

pictureFile.createNewFile();

Camera.Parameters parameters = camera.getParameters();

Size size = parameters.getPreviewSize();

YuvImage image = new YuvImage(data,

parameters.getPreviewFormat(), size.width, size.height,

null);

FileOutputStream filecon = new FileOutputStream(pictureFile);

image.compressToJpeg(

new Rect(0, 0, image.getWidth(), image.getHeight()),

90, filecon);

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

参考:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值