Android图形库Skia(一)-基本测试生成PNG图片

基于淺談 Google Skia 圖形處理引擎Skia Demo Build。 Skia是一个跨平台的图形库,目前使用在Android中,同样也有PC版本,这里测试一下以了解其内涵。

1.PC版本测试:

首先:sudo apt-get install libfreetype6-dev libpng-dev

# 1.下载 Skia 
git clone [email protected]:google/skia.git
# 2.切换到老版本 参考ubuntu 移植SKIA的日期进行回退

git reset --hard 0e2810be95d3f1aa95c341521d3f514eb9e9ebde

# 注:不使用git的话,也可以点击skia_0e2810b下载
# 3.查看编译帮助
$ make help
Targets:
    : 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

显示的是一张图片如下:

Android图形库Skia(一)-基本测试生成PNG图片_第1张图片 

测试程序:

/* Simple vector graphics demo utilizing Skia toolkit.
 * Authored by Jim Huang 
 */
#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上查看和之前实验效果一样。以后会加入freetypeframebuffer。最终实现撇开Android系统来实现简单图形字体显示。

如果编译最新版本的skia,参考这里How to check out and build Skia on Linux。


更:

-libpng如果有问题可以替换为`pkg-config libpng --libs`

Skia & FreeType Android 2D Graphics Essentials

很好的文章。


你可能感兴趣的:(Android)