当前位置:首页 » 安卓系统 » ffmpegandroidh264

ffmpegandroidh264

发布时间: 2023-12-04 14:07:15

㈠ 安卓开发怎么将和h264文件解码播放

如题所示,我想将摄像头采集的数据进行h.264硬编码,我想知道Android是如何对视频数据进行硬件编码的
目前已经知道的方案有:
1、用Android4.1 API MediaCodec来对视频数据进行编码

http://stackoverflow.com/q/17232477/2293921
此种方式我测试了,并未成功,目前一直卡在这里,如果你等帮助我,我将非常感激

2、通过MediaRecorder方式对数据进行编码

具体可参考 http://blog.csdn.net/zblue78/article/details/6083374

3、通过移植ffmpeg

这种方式没接触过,也不了解

可能还有一些其他的方式来对视频硬编码,如果你了解一下,感谢分享!

综上,我更倾向于1的方式去做

我来回答

Android , MediaCodec , 硬编码

post_newreply

//$(\'note_\').focus();
function succeedhandle_vfastpost(url, message, param) {
$(\'vmessage\').value = \'\';
succeedhandle_fastpost(url, message, param);
showCreditPrompt();
}
var vf_tips = \'#在这里快速回复#\';
$(\'vmessage\').value = vf_tips;
$(\'vmessage\').style.color = \'#CDCDCD\';
$(\'vmessage\').onclick = function() {
if($(\'vmessage\').value==vf_tips) {
$(\'vmessage\').value=\'\';
$(\'vmessage\').style.color=\"#000\";
}
}
$(\'vmessage\').onblur = function() {
if(!$(\'vmessage\').value) {
$(\'vmessage\').value=vf_tips;
$(\'vmessage\').style.color=\"#CDCDCD\";
}
}
$(\'vreplysubmit\').onclick = function() {
if($(\'vmessage\').value == vf_tips) {
return false;
}
}

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

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.PixelsFromBuffer(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());
}
}
}

}
}

}

热点内容
苹果8plus什么配置 发布:2024-11-29 14:16:36 浏览:677
androidmvp结构 发布:2024-11-29 14:16:34 浏览:535
androidsqlite命令 发布:2024-11-29 14:04:38 浏览:155
信用卡分期算法 发布:2024-11-29 13:50:56 浏览:807
安卓手机dll文件为什么打不开 发布:2024-11-29 13:40:49 浏览:1001
百分之五十石碳酸怎么配置 发布:2024-11-29 13:38:56 浏览:972
我的世界服务器如何装资源包 发布:2024-11-29 13:25:48 浏览:20
mc服务器的ip是什么 发布:2024-11-29 13:23:33 浏览:568
python的request模块 发布:2024-11-29 13:20:56 浏览:659
android编译环境搭建 发布:2024-11-29 13:04:46 浏览:893