如何将摄像头数据实时使用h264格式编码传送出去

 我来答
一骑当后
2015-02-06 · 知道合伙人数码行家
一骑当后
知道合伙人数码行家
采纳数:40299 获赞数:306428
网络、设备维护、电路、弱电检测。

向TA提问 私信TA
展开全部
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;
import android.view.SurfaceHolder.Callback;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.Bitmap.Config;
import android.hardware.Camera;

public class AndroidVideo extends Activity implements Callback,
Camera.PictureCallback {
private SurfaceView mSurfaceView = null;
private SurfaceHolder mSurfaceHolder = null;
private Camera mCamera = null;

int width = 352; // 此处设定不同的分辨率
int height = 288;
private boolean mPreviewRunning = false;
byte [] mPixel = new byte[width*height*2];

ByteBuffer buffer = ByteBuffer.wrap( mPixel );
Bitmap VideoBit = Bitmap.createBitmap(width, height, Config.RGB_565);

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

getWindow().setFormat(PixelFormat.TRANSLUCENT);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.camera);

mSurfaceView = (SurfaceView) this.findViewById(R.id.surface_camera);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

}

@Override
public void onPictureTaken(byte[] data, Camera camera) {

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
if (mPreviewRunning) {
mCamera.stopPreview();
}
Camera.Parameters p = mCamera.getParameters();
p.setPreviewSize(352, 288);
mCamera.setPreviewCallback(new H264Encoder(352, 288));
mCamera.setParameters(p);
try {
mCamera.setPreviewDisplay(holder);
} catch (Exception ex) {
}
mCamera.startPreview();
mPreviewRunning = true;
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
mCamera = Camera.open();
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {

if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mPreviewRunning = false;
mCamera.release();
mCamera = null;
}

}

@Override
protected void onDestroy() {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}

public void onConfigurationChanged(Configuration newConfig) {
try {
super.onConfigurationChanged(newConfig);
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
}
} catch (Exception ex) {
}
}

protected void onDraw(Canvas canvas) {

// Bitmap tmpBit = Bitmap.createBitmap(mPixel, 320, 480, Bitmap.Config.RGB_565);//.ARGB_8888);

VideoBit.copyPixelsFromBuffer(buffer);//makeBuffer(data565, N));

canvas.drawBitmap(VideoBit, 0, 0, null);
}
}

class H264Encoder implements Camera.PreviewCallback {
long encoder=0;
long count=0;
long decoder=0;
RandomAccessFile raf=null;
private foxSocket foxsocket=null;
byte[] h264Buff =null;
static {
System.loadLibrary("H264Android");
//System.loadLibrary("ffmpeg");
//
}
private H264Encoder(){};

public H264Encoder(int width, int height) {
foxsocket=new foxSocket();
encoder = CompressBegin(width, height);

//decoder = InitDecoder();
h264Buff = new byte[width * height *2];
count = 0;
try {
File file = new File("/sdcard/camera.h264");
raf = new RandomAccessFile(file, "rw");
} catch (Exception ex) {
Log.v("System.out", ex.toString());
}

};
@Override
protected void finalize()
{
CompressEnd(encoder);
Log.v("System.out", "finalize");
if (null != raf)
{
try {
raf.close();
} catch (Exception ex) {
Log.v("System.out", ex.toString());
}
}
try {
super.finalize();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private native long CompressBegin(int width,int height);
private native int CompressBuffer(long encoder, int type,byte[] in, int insize,byte[] out);
private native int CompressEnd(long encoder);

public native long InitDecoder();
public native int UninitDecoder(long encoder);
public native int DecoderNal(long encoder, byte[] in, int insize, byte[] out);
public native int GetH264Width(long encoder);
public native int GetH264Height(long encoder);
public static native int ntohl(int i);
public static native int htonl(int i);
public static native short ntohs(short i);
public static native short htons(short i);

@Override
public void onPreviewFrame(byte[] data, Camera camera) {
if (count<50)
{
int result=CompressBuffer(encoder, -1, data, data.length,h264Buff);
try {
if (result>0)
{
raf.write(h264Buff, 0, result);
foxsocket.UDP_SendBufData("192.168.1.17", 6000, h264Buff, result);
}
count++;
} catch (Exception ex) {
Log.v("System.out", ex.toString());
}
}
else
{
if (count!=9999)
{
count = 9999;
CompressEnd(encoder);
Log.v("System.out", "finalize");
if (null != raf)
{
try {
raf.close();
} catch (Exception ex) {
Log.v("System.out", ex.toString());
}
}
}

}
}

}
高骏(北京)科技有限公司
2020-04-29 广告
公网传输编解码建议选择高骏(北京)科技有限公司,价格合理,品质高服务好。高骏(北京)科技有限公司简称高骏科技。Cogent(高骏科技)创立于 2011年,始终致力于核心技术和创新性产品的自主研发,目前已成为国际知名的无线视音频传输与通信产品... 点击进入详情页
本回答由高骏(北京)科技有限公司提供
V信xuu4567
2015-02-06 · TA获得超过200个赞
知道答主
回答量:102
采纳率:0%
帮助的人:88.5万
展开全部
包头采用ascii,内容用的是内容本身的编码,比如二进制数据
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式