zoukankan      html  css  js  c++  java
  • Android 屏幕截图(底层实现方式)


    加载底层库ScreenCap.java:

    public class ScreenCap {
    
    	static {
    		System.loadLibrary("scrcap");
    	}
    
    	static native void captureScreenToFile(String fileName);
    }
    

    广播接收器:

    public class ScreenCapReceiver extends BroadcastReceiver {
    
    	private static final String LOG_TAG = "ScreenCapReceiver";
    
    	@Override
    	public void onReceive(Context context, Intent intent) {
    		// Temp code, should not in main thread
    		Log.d(LOG_TAG, "generate file name");
                    //Checking external storage
    		boolean mExternalStorageWriteable = false;
    		String state = Environment.getExternalStorageState();
    		if (Environment.MEDIA_MOUNTED.equals(state)) {
    			mExternalStorageWriteable = true;
    		} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    			mExternalStorageWriteable = false;
    		} else {
    			mExternalStorageWriteable = false;
    		}
    
    		if (!mExternalStorageWriteable)
    	            return;
    
    		File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "SCRCAP_"+System.currentTimeMillis()+".png");
    
    		Log.d(LOG_TAG, "Capture screen to : " + file.getAbsolutePath());
    		
    		ScreenCap.captureScreenToFile(file.getAbsolutePath());
    
    		Log.d(LOG_TAG, "screen captured");
    	}
    
    }

    ScreenCap.cpp:

    #include <utils/Log.h>
    
    #include <binder/IPCThreadState.h>
    #include <binder/ProcessState.h>
    #include <binder/IServiceManager.h>
    
    #include <binder/IMemory.h>
    #include <surfaceflinger/ISurfaceComposer.h>
    
    #include <SkImageEncoder.h>
    #include <SkBitmap.h>
    
    #include "com_cust_android_screencap_ScreenCap.h"
    
    using namespace android;
    /*
     * Class:     com_cust_android_screencap_ScreenCap
     * Method:    captureScreenToFile
     * Signature: (Ljava/lang/String;)V
     */
    JNIEXPORT void JNICALL Java_com_cust_android_screencap_ScreenCap_captureScreenToFile
      (JNIEnv *env, jclass clazz, jstring fileName) {
    
        const String16 name("SurfaceFlinger");
        sp<ISurfaceComposer> composer;
        getService(name, &composer);
    
        sp<IMemoryHeap> heap;
        uint32_t w, h;
        PixelFormat f;
        status_t err = composer->captureScreen(0, &heap, &w, &h, &f, 0, 0);
        if (err != NO_ERROR) {
            fprintf(stderr, "screen capture failed: %s
    ", strerror(-err));
            return;
        }
    
        LOGD("screen capture success: w=%u, h=%u, pixels=%p
    ",
                w, h, heap->getBase());
    
        SkBitmap b;
        b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
        b.setPixels(heap->getBase());
        SkImageEncoder::EncodeFile(env->GetStringUTFChars(fileName, 0), b,
                SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
    
    }
    
    JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
        return JNI_VERSION_1_6;
    }
    
    JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
    
    }
    


    下载源码:http://download.csdn.net/detail/weiyirong/5836997

  • 相关阅读:
    背包系列 hdu3449 有依赖背包
    背包系列 hdu 3535 分组背包
    屏蔽scrollview的滚动
    高精度算法代码
    输入法出现时,中间固定,底部上移的代码
    排序之分治排序
    排序之双向冒泡排序
    Miller Rabin 大素数测试
    来聊聊WWDC 苹果大会上的那些黑科技
    不想成为好leader的程序猿不是好攻城狮
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3225746.html
Copyright © 2011-2022 走看看