Android圖形庫Skia(一)-基本測試產生PNG圖片,

來源:互聯網
上載者:User

Android圖形庫Skia(一)-基本測試產生PNG圖片,

基於淺談 Google Skia 圖形處理引擎和Skia Demo Build。 Skia是一個跨平台的圖形庫,目前使用在Android中,同樣也有PC版本,這裡測試一下以瞭解其內涵。

1.PC版本測試:

# 1.下載 Skia 

git clone git@github.com:google/skia.git

# 2.切換到老版本 參考ubuntu 移植SKIA的日期進行回退

git reset --hard 0e2810be95d3f1aa95c341521d3f514eb9e9ebde

# 3.查看編譯協助

$ make help

Targets:

    <default>: out/libskia.a

    bench: out/bench/bench

    gm: out/gm/gm

    skimage: out/tools/skimage

    skhello: out/tools/skhello

    tests: out/tests/tests

    clean: removes entire out/ directory

    help: this text

Options: (after make, or in bash shell)

    SKIA_DEBUG=true for debug build

    SKIA_SCALAR=fixed for fixed-point build

    SKIA_BUILD_FOR=mac for mac build (e.g. CG for image decoding)

SKIA_PDF_SUPPORT=false to disable the pdf generation backend

# 3.進行編譯

$ make SKIA_BUILD_FOR=linux

# 4.將Skia Demo Build的例子儲存為test-skia.c。

# 5.編譯測試程式

$ g++ \

        -I./include \

        -I./include/core \

        -I./include/images \

        -I./include/config \

        -Wall -o test-skia test-skia.c \

         out/src/images/SkImageDecoder_libpng.o out/libskia.a \

        -lpng -lpthread -lfreetype -g

# 運行

$ ./test-skia

# 查看測試結果

$ eog snapshot.png

顯示的是一張圖片如下:

 

測試程式:

/* Simple vector graphics demo utilizing Skia toolkit.

 * Authored by Jim Huang <jserv.tw@gmail.com>

 */

 

#include "SkBitmap.h"

#include "SkDevice.h"

#include "SkPaint.h"

#include "SkRect.h"

#include "SkImageEncoder.h"

 

int main()

{

// Declare a raster bitmap, which has an integer width and height,

// and a format (config), and a pointer to the actual pixels.

// Bitmaps can be drawn into a SkCanvas, but they are also used to

// specify the target of a SkCanvas' drawing operations.

SkBitmap bitmap;

bitmap.setConfig(SkBitmap::kARGB_8888_Config, 200, 200);

bitmap.allocPixels();

 

// A Canvas encapsulates all of the state about drawing into a

// device (bitmap).  This includes a reference to the device itself,

// and a stack of matrix/clip values. For any given draw call (e.g.

// drawRect), the geometry of the object being drawn is transformed

// by the concatenation of all the matrices in the stack. The

// transformed geometry is clipped by the intersection of all of the

// clips in the stack.

SkCanvas canvas(bitmap);

 

// SkPaint class holds the style and color information about how to

// draw geometries, text and bitmaps.

SkPaint paint;

 

// SkIRect holds four 32 bit integer coordinates for a rectangle.

 

SkRect r;

 

paint.setARGB(255, 255, 0, 0);

r.set(25, 25, 145, 145);

canvas.drawRect(r, paint); /** Draw the specified rectangle using

       the specified paint. The rectangle

       will be filled or stroked based on

       the Style in the paint. */

 

paint.setARGB(255, 0, 255, 0);

r.offset(20, 20);

canvas.drawRect(r, paint);

 

paint.setARGB(255, 0, 0, 255);

r.offset(20, 20);

canvas.drawRect(r, paint);

 

// SkImageEncoder is the base class for encoding compressed images

// from a specific SkBitmap.

SkImageEncoder::EncodeFile("snapshot.png", bitmap,

SkImageEncoder::kPNG_Type,

/* Quality ranges from 0..100 */ 100);

return 0;

}

 

2.在Android中測試

寫了一個Android.mk內容如下:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

 

LOCAL_MODULE_TAGS := optional

 

LOCAL_MODULE := skia_test2

 

#Can't use 'LOCAL_SRC_FILES := $(call all-subdir-cpp-files)'

LOCAL_SRC_FILES := skia_test2.cpp

 

LOCAL_C_INCLUDES := \

    external/skia/include/core/ \

    external/skia/include/images/ 

 

LOCAL_SHARED_LIBRARIES := \

     libskia

 

include $(BUILD_EXECUTABLE)

可以原封不動地將PC上的測試源碼編譯成適合Android的測試程式。將其放到/system/bin目錄下。如下執行:

$ cd sdcard

$ skia_test2

會在/sdcard目錄下同樣產生snapshot.png,傳到PC上查看和之前實驗效果一樣。以後會加入freetype和framebuffer。最終實現撇開Android系統來實現簡單圖形字型顯示。




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.