Simple Application of skia APIs

Source: Internet
Author: User
Tags drawtext transparent color xml parser skia

Address: http://blog.csdn.net/cuiyan0214/archive/2010/10/25/5964739.aspx

 

 

 

1 skia plotting Overview

 

Skia is a graphic library of Google's underlying graphics, text, images, animations, and other aspects. It is the engine of the Image System in Android. Skia is used as a third-party software in the external Directory: external/skia /. The source files and some header files of skia are in the src directory, and the exported header files are in the include directory.

The following classes are mainly used for Drawing Images Using skia APIs:

Skbitmap, skcanvas, skpaint, and skrect. skbitmap is used to set pixels, skcanvas is written to bitmap, skpaint is used to set colors and styles, and skrect is used to draw rectangles. The implementation code is mainly in the src/Core directory.

 

2. Steps for drawing with skia

 

A) define a 32-Bit Bitmap and initialize it.
Skbitmap bitmap;

Bitmap. setconfig (skbitmap: kargb_8888_config, 200,200 );

Setconfig is used to set the bitmap format. The prototype is void setconfig (config, int width, int height, int rowbytes = 0)

Config is a Data Structure
Enum config {
Kno_config, // uncertain bitmap format
Ka1_config, // 1-bit (black, white) bitmap
Ka8_config, // 8-bit (black, white) bitmap
Kindex8_config, // similar to the color index table in windows. For details, see the skcolortable class structure.
Krgb_565_config, // a 16-bit 565-Bit Bitmap. For more information, see the skcolorpriv. h file.
Kargb_4444_config, // a 16-bit 4444-Bit Bitmap. For more information, see the skcolorpriv. h file.
Kargb_8888_config, // 32-bit pixel 8888 format bitmap. For more information, see the skcolorpriv. h file.
Krle_index8_config,
Kconfigcount
};

B) Allocate space occupied by bitmap

Bitmap. allocpixels ()
In fact, allocpixels is an overloaded function, and its prototype is bool allocpixels (skcolortable * ctable = NULL)
The ctable parameter is a color index table, which is generally null.

C) Specify the output device

Skcanvas canvas (New skdevice (Bitmap ));
Canvas is a multi-constructor. Its prototype is

Explicit skcanvas (const skbitmap & Bitmap ),

Explicit skcanvas (skdevice * Device = NULL)
The explicit it parameter indicates that type conversion is not allowed.
The output device can be a context device or a bitmap.

D) style of device painting
Paint paint;
Skrect R;
Paint. setargb (255,255, 0, 0 );
R. Set (25, 25,145,145 );
Canvas. drawrect (R, paint );
Paint can specify the color of the drawing, the size and alignment of the text, the encoding format, and so on. Because the previous bitmap format is set to kargb_8888_config, set the color setargb (255,255, 0, 0). The first parameter is the transparent color channel. The other three parameters are R, G, and B. R sets the range to be drawn, and draws a square of the specified area through drawrect.

In this way, a red rectangle is successfully drawn.

Skcanvas mainly provides three painting functions:

A Basic Graph Drawing (such as the drawargb and drawline functions)

B. Drawing image files (drawbitmap function)

C text rendering (drawtext function)

Related APIs include:

Canvas. drawrect (rect, paint );
Canvas. drawoval (oval, paint );
Canvas. drawcircle (X, Y, radius, paint );
Canvas. drawroundrect (rect, RX, Ry, paint );
Canvas. drawpath (path, paint );
Canvas. drawbitmap (bitmap, X, Y, & paint );
Canvas. drawbitmaprect (bitmap, & srcrect, dstrect, & paint );
Canvas. drawbitmapmatrix (bitmap, matrix, & paint );
Canvas. drawtext (text, length, X, Y, paint );
Canvas. drawpostext (text, length, POS [], paint );
Canvas. drawtextonpath (text, length, path, paint );

E)

Routine

 

I) point, line, circle, and text

# Include "skbitmap. H"

# Include "skdevice. H"

# Include "skpaint. H"

# Include "skrect. H"

# Include "skimageencoder. H"

# Include "sktypeface. H"

 

Using namespace STD;

 

Int main ()

{

Skbitmap bitmap;

Bitmap. setconfig (skbitmap: kargb_8888_config, 320,240 );

Bitmap. allocpixels ();

Skcanvas canvas (New skdevice (Bitmap ));

Skpaint paint;

// Draw points with red.

Paint. setargb (255,255, 0, 0 );

Paint. setstrokewidth (4 );

Canvas. drawpoint (40, 30, paint );

Canvas. drawpoint (80, 60, paint );

Canvas. drawpoint (, paint );

// Draw a line with green.

Paint. setargb (255, 0,255, 0 );

Paint. setstrokewidth (4 );

Canvas. drawline (320,110, paint );

// Draw a circle with Bule.

Paint. setargb (255, 0, 0,255 );

Canvas. drawcircle (80,180, 50, paint );

// Draw text with red

Sktypeface * font = sktypeface: createfromfile ("simkai. TTF ");

If (font)

{

Paint. setargb (255,255, 0, 0 );

Paint. settypeface (font );

Paint. settextsize (24 );

Canvas. drawtext ("Hello! :) ", 8,200,180, paint );

}

Skimageencoder: encodefile ("snapshot.png", bitmap, skimageencoder: kpng_type, 100 );

Return 0;

}

After the program is executed, the following output is displayed:

 

 

 

Ii) image coding/Decoding

This routine currently supports only. PNG images and. jpg images.

# Include "skbitmap. H"

# Include "skdevice. H"

# Include "skpaint. H"

 

# Include "skrect. H"

# Include "skimageencoder. H"

# Include "skimagedecoder. H"

# Include <iostream>

 

Using namespace STD;

 

Int main ()

{

Int ret =-1;

Skbitmap bitmap;

 

// Skimagedecoder

Ret = skimagedecoder: decodefile ("./old.png", & Bitmap );

Cout <"Get the decode type =" <bitmap. config () <Endl;

 

// Skimageencoder

Ret = skimageencoder: encodefile ("new1.png", bitmap, skimageencoder: kpng_type, 100 );

Cout <"encode data to PNG result =" <RET <Endl;

 

Return 0;

}

Skimagedecoder: decodefile ("./old.png", & Bitmap );

Convert PNG to a bitmap format and place the data in the bitmap variable.
Skimageencoder: encodefile ("snapshot.png", bitmap, skimageencoder: kpng_type,/* Quality ranges from 0 .. 100 */100 );

Output the data encoding in bitmap. PNG format. The first parameter is the PNG file path, the second is the specified output bitmap, the third is the file type, and the fourth parameter specifies the quality of the output bitmap. The value range is 0 .. 100. The default value is 80.

 

3. image effects

The src/effects directory files mainly implement some special effects on graphics and images, including masks, relief, blur, filters, gradient, discretization, transparency, and various path effects.

 

4. Animation

The src/animator directory file mainly implements the animation effect of skia, which is not supported by Android.

 

5. UI Library

The src/view directory creates a set of UI libraries.
Widgets include window, menu, Textbox, listview, progressbar, widget, scrollbar, taglist, and image.

 

6 others

A) src/GL Directory: skia calls OpenGL or OpenGL ES to implement 3D effects.
If Mac is defined, OpenGL is used. If Android is defined, the esgl 3D graphics library on the embedded system is used.

B) src/images Directory: Mainly skimagedecoder, skimageencoder, and skmovie. Mainly used to process images. The types of images that can be processed include BMP,

JPEG/pvjpeg, PNG, ICO, and skmovie are used to process GIF animation.

C) src/opts Directory: Performance Optimization code.

D) src/pdf Directory: Processes PDF documents and uses a fpdfemb library.

E) src/ports Directory: This part is the implementation of some skia interfaces on different systems and platform-related code, such as fonts, threads, and time. It mainly includes the following parts: font, event, file, thread, time, xmlparser

These and skia interfaces must be implemented for different operating systems.

F) src/SVG Directory: vector image, which is not supported by Android.
Sksvgpath, sksvgpolyline, sksvgrect, sksvgtext, sksvgline, sksvgimage, sksvgellipse, and so on.

G) src/text Directory :???

H) src/utils Directory: it is a helper tool class.
Skcamera, skcolormatrix, skosfile, skproxycanvas, skinterpolator, and other files.

I) src/xml: this is the part for processing XML data. skia only wraps the XML Parser here, the specific implementation of the XML Parser needs to be implemented based on different operating systems and host programs.

J) third-party library

In addition to all of its own files, skia also uses some third-party libraries and contains many Linux header files.

By analyzing the skia source program, it is found that skia mainly uses the following third-party libraries:
Zlib for data compression and decompression
Jpeglib for processing JPEG image encoding and decoding
Pnglib, encoding and decoding of PNG Images
Giflib, processing GIF images
Fpdfemb, processing PDF documents

Skia also needs some header files in Linux/Unix (more files may be needed ):
Stdint. h
Unistd. h
Features. h
Cdefs. h
Stubs. h
Posix_opt.h
Types. h
Wordsize. h
Typesizes. h
Confname. h
Getopt. h
Mman. h

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/cuiyan0214/archive/2010/10/25/5964739.aspx

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.