编码器使用的是x264的开源库,
很容易看懂的
简单的封装了一个JNI库
编码库在BBS里 CSDN的资源太难用了
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=52739&extra=
x264的编译放方法
export ARM_ROOT=$ANDROID_NDK_ROOT
export ARM_INC=$ARM_ROOT/build/platforms/android-5/arch-arm/usr/include/
export ARM_LIB=$ARM_ROOT/build/platforms/android-5/arch-arm/usr/lib/
export ARM_TOOL=$ARM_ROOT/build/prebuilt/windows/arm-eabi-4.4.0
export ARM_LIBO=$ARM_TOOL/lib/gcc/arm-eabi/4.4.0
export PATH=$ARM_TOOL/bin:$PATH
export ARM_PRE=arm-eabi
./configure --prefix=/home/egmkang/libx264 --enable-shared \
-disable-asm --host=arm-linux --cross-prefix=arm-eabi-\
--extra-cflags=" -I$ARM_INC -fPIC -DANDROID -fpic -mthumb-interwork -ffunction-sections -funwind-tables -fstack-protector -fno-short-enums -D__ARM_ARCH_5__ -D__ARM_ARCH_5T__ -D__ARM_ARCH_5E__ -D__ARM_ARCH_5TE__ -Wno-psabi -march=armv5te -mtune=xscale -msoft-float -mthumb -Os -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -DANDROID -Wa,--noexecstack -MMD -MP "\
--extra-ldflags="-nostdlib -Bdynamic -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,nocopyreloc -Wl,-soname,/system/lib/libz.so -Wl,-rpath-link=$ARM_LIB,-dynamic-linker=/system/bin/linker -L$ARM_LIB -nostdlib $ARM_LIB/crtbegin_dynamic.o $ARM_LIB/crtend_android.o -lc -lm -ldl -lgcc"
这里生成的是x264的静态库
整个工程唯一有点麻烦的是 生成 JNI 动态库的时候 报错 。。
后来发现是少链接了一个库,
于是根据x264的编译方法 在Android.mk添加一些配置就可以了。当然这就是难点,在网上查了很多都没有结果。
有些目录的参数自己调整哈
我把前面生成的libx264.a 和 x264.h 文件放到jni的libx264目录下了 有问题自己调整
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES +=$(LOCAL_PATH)/libx264/include
LOCAL_MODULE := H264Android
LOCAL_SRC_FILES := H264Android.c
LOCAL_LDFLAGS += $(LOCAL_PATH)/libx264/lib/libx264.a
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -lgcc
include $(BUILD_SHARED_LIBRARY)
估计很多人都会发现很熟悉 嘻嘻 这个就是根据:http://www.rosoo.net/a/201106/14632.html
改的 连文件名字都没有换!!比较懒
另: 编码的效率很低下啊
AndroidVideo.java
- import java.io.File;
- import java.io.RandomAccessFile;
- 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.PixelFormat;
- 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;
- private boolean mPreviewRunning = false;
- 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;
- }
- }
- 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) {
- }
- }
- }
- class H264Encoder implements Camera.PreviewCallback {
- long encoder=0;
- RandomAccessFile raf=null;
- byte[] h264Buff =null;
- static {
- System.loadLibrary("H264Android");
- }
- private H264Encoder(){};
-
- public H264Encoder(int width, int height) {
- encoder = CompressBegin(width, height);
- h264Buff = new byte[width * height *8];
- try {
- File file = new File("/sdcard/camera.h264");
- raf = new RandomAccessFile(file, "rw");
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
-
- };
- protected void finalize()
- {
- CompressEnd(encoder);
- if (null != raf)
- {
- try {
- raf.close();
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
- }
- try {
- super.finalize();
- } catch (Throwable e) {
-
- 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);
- @Override
- public void onPreviewFrame(byte[] data, Camera camera) {
-
- int result=CompressBuffer(encoder, -1, data, data.length,h264Buff);
- try {
- if (result>0)
- raf.write(h264Buff, 0, result);
- } catch (Exception ex) {
- Log.v("System.out", ex.toString());
- }
- }
-
- }
(zblue78) |