android截屏示例,适合流媒体 MediaPlayer截屏
16lz
2022-03-15
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)
更多相关文章
- Error:(16, 0) Gradle DSL method not found: 'android()'
- 解决Cocos2d-x3.1编译生成Android程序出现Android(安卓)NDK:Abor
- Android(安卓)获取 IP 地址
- Android(安卓)Studio打包时出现transformClassesWithDexForRelea
- android 学习笔记有用代码片段(2)
- Android震动代码解读
- Memory Analyzer(MAT)打开android eclipse Dump内存文件出现 Unkno
- android中菜单的使用
- Android隐藏ListView的滚动条