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*2jint 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 -llogLOCAL_MODULE    := scrcapinclude $(BUILD_SHARED_LIBRARY)




更多相关文章

  1. android中如何在代码中直接设置View的layout_weight属性
  2. Android震动代码解读
  3. android 学习笔记有用代码片段(2)
  4. android 关于gallery 的平滑代码滚动
  5. Android客户端自动更新代码
  6. [CSDN]Android应用程序启动过程源代码分析
  7. android连续按两次返回退出程序(完整代码)
  8. Android Java代码执行adb Shell命令
  9. 探寻 Android 代码抄袭细节,情节还不算严重

随机推荐

  1. 扒一扒 @SpringBootApplication 注解背后
  2. 解密ThreadLocal
  3. JS内存泄漏排查方法
  4. 函数和递归
  5. 视频演示:好家伙,我直接好家伙!
  6. 深入React
  7. 今晚,我等你 ~
  8. 分布式系统的事务处理
  9. 都想学大数据开发?年轻人耗子尾汁吧~
  10. 今天的南京,很冷很冷