Capture Screen in Gingerbread(Android 2.3實現截屏)

來源:互聯網
上載者:User

本文是採用zmyde2010的方法,在sunsumg i9100上啟動截屏服務,在此記錄操作過程。

zmyde2010文章的連結:http://blog.csdn.net/zmyde2010/article/details/6925498

此截屏方法是參照Android源碼

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

 

 1 /*
 2  * Copyright (C) 2010 The Android Open Source Project
 3  *
 4  * Licensed under the Apache License, Version 2.0 (the "License");
 5  * you may not use this file except in compliance with the License.
 6  * You may obtain a copy of the License at
 7  *
 8  *      http://www.apache.org/licenses/LICENSE-2.0
 9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <utils/Log.h>
18 
19 #include <binder/IPCThreadState.h>
20 #include <binder/ProcessState.h>
21 #include <binder/IServiceManager.h>
22 
23 #include <binder/IMemory.h>
24 #include <surfaceflinger/ISurfaceComposer.h>
25 
26 #include <SkImageEncoder.h>
27 #include <SkBitmap.h>
28 
29 using namespace android;
30 
31 int main(int argc, char** argv)
32 {
33     if (argc != 2) {
34         printf("usage: %s path\n", argv[0]);
35         exit(0);
36     }
37 
38     const String16 name("SurfaceFlinger");
39     sp<ISurfaceComposer> composer;
40     getService(name, &composer);
41 
42     sp<IMemoryHeap> heap;
43     uint32_t w, h;
44     PixelFormat f;
45     status_t err = composer->captureScreen(0, &heap, &w, &h, &f, 0, 0);
46     if (err != NO_ERROR) {
47         fprintf(stderr, "screen capture failed: %s\n", strerror(-err));
48         exit(0);
49     }
50 
51     printf("screen capture success: w=%u, h=%u, pixels=%p\n",
52             w, h, heap->getBase());
53 
54     printf("saving file as PNG in %s ...\n", argv[1]);
55 
56     SkBitmap b;
57     b.setConfig(SkBitmap::kARGB_8888_Config, w, h);
58     b.setPixels(heap->getBase());
59     SkImageEncoder::EncodeFile(argv[1], b,
60             SkImageEncoder::kPNG_Type, SkImageEncoder::kDefaultQuality);
61 
62     return 0;
63 }

下載該文中的提供的代碼,我所採用的方法是將代碼加入到了CM的源碼中。

編譯

為保持源碼的一致性,將screencap代碼放在packages/apps目錄下。

接下來就是編譯系統,使新產生的系統包內含screencap程式。

要使目標源碼編譯時間包含我們添加進去的程式,需要在bulid/target/product/generic.mk檔案中將package name添加進去:

PRODUCT_PACKAGES := /
 AccountAndSyncSettings /
 CarHome /
 DeskClock /
 ……
 SyncProvider /
 Screencap

 

然後在Android.mk中注意包含LOCAL_MODULE_TAGS := optional 語句。

完成這些步驟之後,就可以開始編譯源碼燒寫系統了。

燒好系統後, 在系統中,截屏程式預設屬性為開機自啟動,作為系統服務等待調用。

在自己的應用程式中,使用如下語句啟用服務

   sendBroadcast(new Intent("com.android.CAPTURE_SCREEN"));

在sd卡目錄下產生圖片。

 這樣,wayne將截屏程式在i9100手機中就運行成功了,還是一樣,歡迎指正!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.