Android: How to Capture Screen in Gingerbread(2.3中实现截屏) .

原帖地址:http://blog.csdn.net/zmyde2010/article/details/6925498#reply    感谢TODO: Android; Linux; Cloud;

示例代码在:

frameworks\base\services\surfaceflinger\tests\screencap\screencap.cpp

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#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>

using namespace android;

int main(int argc, char** argv)
{
    if (argc != 2) {
        printf("usage: %s path\n", argv[0]);
        exit(0);
    }

    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\n", strerror(-err));
        exit(0);
    }

    printf("screen capture success: w=%u, h=%u, pixels=%p\n",
            w, h, heap->getBase());

    printf("saving file as PNG in %s ...\n", argv[1]);

    SkBitmap b;
    b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
    b.setPixels(heap->getBase());
    SkImageEncoder::EncodeFile(argv[1], b,
            SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);

    return 0;
}


编译后生成 /system/bin/test-screencap

测试时终端输入 test-screencap /mnt/sdcard/scapxx.png

可以看到SD卡生成截屏文件scapxx.png,默认大小为屏幕分辨率

如果想把这个功能写到自己的应用里

写个JNI,参考上面代码即可,但记得权限声明

<SPAN style="FONT-SIZE: 24px"><uses-permission android:name="android.permission.READ_FRAME_BUFFER" /></SPAN>


 

贴出JNI代码,或资源下载地址:http://download.csdn.net/detail/zmyde2010/3835127

jni/com_android_ScreenCap_ScreenCapNative.cpp

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#define LOG_TAG "ScreenCapNative-JNI"

//#define LOG_NDEBUG 0

#include "JNIHelp.h"
#include "jni.h"

#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>

namespace android {	

static void android_ScreenCap_ScreenCapNative_nativeCaptureScreen(JNIEnv* env,
        jobject obj, jstring file) 
{
	const char *file_path = env->GetStringUTFChars(file, NULL);

    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) {
        LOGE("screen capture failed: %s\n", strerror(-err));
        exit(0);
    }

    LOGD("screen capture success: w=%u, h=%u, pixels=%p\n",
            w, h, heap->getBase());

    LOGD("saving file as PNG in %s ...\n", file_path);

    SkBitmap b;
    b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
    b.setPixels(heap->getBase());
    SkImageEncoder::EncodeFile(file_path, b,
            SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);        	
}

static JNINativeMethod gScreenCapNativeMethods[] = {
    /* name, signature, funcPtr */
    { "nativeCaptureScreen", "(Ljava/lang/String;)V",
            (void*) android_ScreenCap_ScreenCapNative_nativeCaptureScreen },            
};
	
int register_android_ScreenCap_ScreenCapNative(JNIEnv* env) {
    int res = jniRegisterNativeMethods(env, "com/android/ScreenCap/ScreenCapNative",
            gScreenCapNativeMethods, NELEM(gScreenCapNativeMethods));
    LOG_FATAL_IF(res < 0, "Unable to register native methods.");

    return 0;
}	
	
extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved)
{
    JNIEnv* env = NULL;
    jint result = -1;

    if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
        LOGE("GetEnv failed!");
        return result;
    }
    LOG_ASSERT(env, "Could not retrieve the env!");

    register_android_ScreenCap_ScreenCapNative(env);

    return JNI_VERSION_1_4;
}	
	
} /* namespace android */


jni/Android.mk

 

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_SRC_FILES := com_android_ScreenCap_ScreenCapNative.cpp

LOCAL_C_INCLUDES += \
	$(JNI_H_INCLUDE)

LOCAL_SHARED_LIBRARIES := \
	libnativehelper \
	libcutils \
	libutils \
	libbinder \
	libskia \
    libui \
    libsurfaceflinger_client

LOCAL_C_INCLUDES += \
	external/skia/include/core \
	external/skia/include/effects \
	external/skia/include/images \
	external/skia/src/ports \
	external/skia/include/utils

LOCAL_MODULE:= libscreencapjni
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false

include $(BUILD_SHARED_LIBRARY)    


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
此应用程序 不仅介绍了使用 Microsoft 基础类的基本知识, 而且是编写应用程序的起点。 此文件包含组成 ScreenCapture 应用程序的每个文件的内容摘要。 ScreenCapture.vcproj 这是使用“应用程序向导”生成的 VC++ 项目的主项目文件。 它包含有关生成文件的 Visual C++ 版本的信息,以及 有关用“应用程序向导”所选择的平台、配置和 项目功能的信息。 ScreenCapture.h 这是应用程序的主头文件。 它包含其他 项目特定的头文件(包括 Resource.h),并声明 CScreenCaptureApp 应用程序类。 ScreenCapture.cpp 这是包含应用程序 类 CScreenCaptureApp 的主应用程序源文件。 ScreenCapture.rc 这是程序使用的所有 Microsoft Windows 资源 的列表。 它包含存储在 RES 子目录 的图标、位图和光标。 可直接在 Microsoft Visual C++ 编辑此文件。 项目资源包含在 2052 。 res\ScreenCapture.ico 这是一个图标文件,用作应用程序的图标。 此 图标包含在主资源文件 ScreenCapture.rc 。 res\ScreenCapture.rc2 此文件包含不由 Microsoft Visual C++ 编辑的资源。 应将所有不能由 资源编辑器编辑的资源放在此文件。 ///////////////////////////////////////////////////////////////////////////// 应用程序向导将创建一个对话框类: ScreenCaptureDlg.h、ScreenCaptureDlg.cpp - 对话框 这些文件包含 CScreenCaptureDlg 类。 此类定义 应用程序主对话框的行为。 此对话框的模板包含在 ScreenCapture.rc ,而此文件可以在 Microsoft Visual C++ 进行编辑。 ///////////////////////////////////////////////////////////////////////////// 其他功能: ActiveX 控件 应用程序支持使用 ActiveX 控件。 打印支持和打印预览支持 应用程序向导已生成了一些代码,通过从 MFC 库调用 CView 类的成员函数来 处理打印、打印设置和打印预览命令。 ///////////////////////////////////////////////////////////////////////////// 其他标准文件: StdAfx.h、StdAfx.cpp 这些文件用于生成名为 ScreenCapture.pch 的预编译头文件 (PCH) 和名为 StdAfx.obj 的预编译类型文件。 Resource.h 这是标准头文件,它定义新资源 ID。 Microsoft Visual C++ 将读取并更新此文件。 ///////////////////////////////////////////////////////////////////////////// 其他说明: 应用程序向导使用“TODO:” 来指示 应添加或自定义的源代码部分。 如果应用程序在共享 DLL 使用 MFC,且应用程序使用的语言不是 操作系统的当前语言,则需要从 Microsoft Visual C++ 光盘上 Win\System 目录下将相应的本地化资源 MFC70XXX.DLL 复制到计算机的 system 或 system32 目录下, 并将其重命名为 MFCLOC.DLL。 (“XXX”代表 语言缩写。 例如,MFC70DEU.DLL 包含翻译成 德语的资源。) 如果不这样做,应用程序的某些 UI 元素 将保留为操作系统的语言。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值