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

}
}

}

熱點內容
c語言文件讀入 發布:2024-07-27 17:22:30 瀏覽:268
200kw負荷如何配置計量 發布:2024-07-27 17:19:59 瀏覽:906
c語言數組的最大長度 發布:2024-07-27 17:04:07 瀏覽:326
安卓手機怎麼變成充電寶 發布:2024-07-27 16:51:02 瀏覽:943
java高級課程 發布:2024-07-27 16:42:01 瀏覽:400
win10電腦怎麼看wifi密碼 發布:2024-07-27 16:03:13 瀏覽:516
豐田探駿哪個配置好 發布:2024-07-27 15:47:13 瀏覽:784
虛擬化的雲存儲 發布:2024-07-27 15:27:36 瀏覽:765
譚浩強的c語言教材 發布:2024-07-27 15:22:08 瀏覽:581
歐盟為什麼要訪問新疆 發布:2024-07-27 15:20:17 瀏覽:973