當前位置:首頁 » 安卓系統 » 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());
}
}
}

}
}

}

熱點內容
超解壓鴨的視頻 發布:2024-02-29 17:11:49 瀏覽:554
c語言刪標點 發布:2024-02-29 17:11:49 瀏覽:807
安卓怎麼抽簽 發布:2024-02-29 17:03:11 瀏覽:427
買單反相機要有什麼配置 發布:2024-02-29 16:48:32 瀏覽:194
語義分析階段在編譯過程中的作用 發布:2024-02-29 16:01:33 瀏覽:290
現代悅納哪個配置有手環 發布:2024-02-29 15:57:24 瀏覽:636
存儲卡FC 發布:2024-02-29 15:39:54 瀏覽:582
win2008建ftp 發布:2024-02-29 15:31:17 瀏覽:856
大數據生成腳本 發布:2024-02-29 15:25:55 瀏覽:630
行李箱怎麼弄密碼 發布:2024-02-29 15:15:06 瀏覽:659