android截屏示例,适合流媒体 MediaPlayer截屏

Java 代码package com.bitmaptest; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Bundle; import android.util.Log; import android.view.View; public class bitmaptest extends Activity { private static final int SCP = 0; private char [] mBuf; private byte [] mByteBuf; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBuf = new char[320*480*3]; mByteBuf = new byte[320*480*2]; setContentView(new myView(this)); } class myView extends View{ public myView(Context context) { super(context); } protected void onDraw(Canvas canvas) { super.onDraw(canvas); byte[] buf = new byte[320*480*2 ]; Log.d("scrcap", "scrcap=------------------------"); if(0 == scrcap(buf)) { Log.d("scrcap", "back"); int[] colors = new int[320*480]; for(int i = 0;i < colors.length;i ++) { int r = 0x000000ff &( ((int)buf[i*2 +1] & 0x000000f8)>>3 ); int g = 0x000000ff &( (((int)buf[i*2 +1] & 0x00000007) << 3) |((int)buf[i*2 ] & 0x000000e0)>>5 ); int b = 0x000000ff &( ((int)buf[i*2] & 0x0000001f) ); colors[i] = 0xff000000 | (r << 19 )| ( g << 10 )| b<<3; } Bitmap.Config config = Bitmap.Config.RGB_565; Bitmap mBitmap = Bitmap.createBitmap(colors,320,480,config); Paint mPaint = new Paint(); canvas.drawBitmap(mBitmap,0, 0, mPaint); } } } public static native int scrcap(byte[] data); static { System.loadLibrary("scrcap"); }; }

JNI 代码 在虚拟机下无法直接使用, fb open错误

#include <jni.h> #include <stdio.h> #include <string.h> #include <malloc.h> #include <unistd.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <android/log.h> #include <sys/mman.h> #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "!!!!!", __VA_ARGS__) #define FB_DEV "/dev/graphics/fb0" #define SIZE 320*480*2 jint Java_com_bitmaptest_bitmaptest_scrcap(JNIEnv* env, jobject thiz,jbyteArray data) { LOGE("--------=================-----------------"); jbyte* pdata = (*env)->GetByteArrayElements(env,data, 0); char buf[SIZE]; int fd = open(FB_DEV,O_RDWR); if(fd <= 0) { LOGE("OPEN fb device open error"); return -1; } void * fb_mem = (void *)mmap(NULL, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(-1L == (int)fb_mem) { LOGE("OPEN fb device mmap error"); return -1; } memcpy(buf,fb_mem,SIZE); munmap(fb_mem,SIZE); close(fd); memcpy(pdata,buf,SIZE); (*env)->ReleaseByteArrayElements(env,data, pdata, 0); return 0; }

JNI makefile

LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_C_INCLUDES += $(JNI_H_INCLUDE) LOCAL_SRC_FILES := scrcap.c LOCAL_LDLIBS:=-L$(SYSROOT)/usr/lib -llog LOCAL_MODULE := scrcap include $(BUILD_SHARED_LIBRARY)




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值