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)