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. Error:(16, 0) Gradle DSL method not found: 'android()'
  2. 解决Cocos2d-x3.1编译生成Android程序出现Android(安卓)NDK:Abor
  3. Android(安卓)获取 IP 地址
  4. Android(安卓)Studio打包时出现transformClassesWithDexForRelea
  5. android 学习笔记有用代码片段(2)
  6. Android震动代码解读
  7. Memory Analyzer(MAT)打开android eclipse Dump内存文件出现 Unkno
  8. android中菜单的使用
  9. Android隐藏ListView的滚动条

随机推荐

  1. Android静态图片人脸识别的完整demo(附完
  2. 【Android开发】Android(安卓)Studio中进
  3. [转]Android(安卓)Power Management
  4. Android中visibility的3个属性说明
  5. android的PreferenceActivity
  6. 教你如何开关Android的APN网络
  7. [转]JS调用Android里面的方法,Android调用
  8. Android(安卓)横竖屏切换保留状态
  9. Android:Serializable接口和Parcelable接
  10. Android(安卓)AndroidX的迁移