当前位置:首页 > 公众号精选 > 嵌入式云IOT技术圈
[导读]点击上方嵌入式开发圈 记得关注我们哦!   在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码。      以下这段代码位于recovery/screen_ui.cpp 1void ScreenRecoveryU

点击上方嵌入式开发圈 记得关注我们哦!

  在上一篇文章中,我们大致的介绍了recovery的启动流程,那么,recovery升级或者做双清的时候,那些图形动画又是如何实现的呢?我们来看看代码。   

  以下这段代码位于recovery/screen_ui.cpp

1void ScreenRecoveryUI::Init()
2{
3    gr_init();
4
5    gr_font_size(&char_width, &char_height);
6
7    text_col = text_row = 0;
8    text_rows = gr_fb_height() / char_height;
9    if (text_rows > kMaxRows) text_rows = kMaxRows;
10    text_top = 1;
11
12    text_cols = gr_fb_width() / char_width;
13    if (text_cols > kMaxCols - 1) text_cols = kMaxCols - 1;
14
15    backgroundIcon[NONE] = NULL;
16    LoadBitmapArray("icon_installing", &installing_frames, &installation);
17    backgroundIcon[INSTALLING_UPDATE] = installing_frames ? installation[0] : NULL;
18    backgroundIcon[ERASING] = backgroundIcon[INSTALLING_UPDATE];
19    LoadBitmap("icon_error", &backgroundIcon[ERROR]);
20    backgroundIcon[NO_COMMAND] = backgroundIcon[ERROR];
21
22    LoadBitmap("progress_empty", &progressBarEmpty);
23    LoadBitmap("progress_fill", &progressBarFill);
24    LoadBitmap("stage_empty", &stageMarkerEmpty);
25    LoadBitmap("stage_fill", &stageMarkerFill);
26
27    LoadLocalizedBitmap("installing_text", &backgroundText[INSTALLING_UPDATE]);
28    LoadLocalizedBitmap("erasing_text", &backgroundText[ERASING]);
29    LoadLocalizedBitmap("no_command_text", &backgroundText[NO_COMMAND]);
30    LoadLocalizedBitmap("error_text", &backgroundText[ERROR]);
31
32    pthread_create(&progress_tNULL, progress_thread, NULL);
33
34    RecoveryUI::Init();
35}

这段代码都做了哪些事情呢?这些recovery初始化图形显示最开始的部分:(1)调用了miniui中的gr_init初始化显示图形相关的步骤,因为recovery是基于framebuffer机制显示的。

(2)调用gr_font_size设置字体显示的大小,然后计算文本显示行列。

(3)接下来就是装载图片了,会调用到LoadBitmapArray和LoadBitmap这两个函数。其中,我们会看到这些函数里图片的名称:

将上面的字符串与下面的图片一一对应:

    那么这些分别是怎么显示的?其中erasing_text是用来显示做清除的时候显示的文字,放大后如下:

    这上面有许许多多的语言版本,我们可以根据需要来选择,这些主要要看接下来初始化文字的代码逻辑。

    其余的图片中,后缀带text的,也和这些是类似的,有出现错误显示的字体error_text,更新系统显示的字体installing_text,没有命令的时候显示的字体no_command_text。

    除了文字显示,我们最关心的就是icon_installing这张图片了,在做系统更新的时候,这个机器人会转动。这不是动画吗?怎么只有一张图片呢?我们找到Android官方网站看看是为什么?原因如下:

 1# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Script to take a set of frames (PNG files) for a recovery animation
15and turn it into a single output image which contains the input frames
16interlaced by row.  Run with the names of all the input frames on the
17command line, in order, followed by the name of the output file."""

18import sys
19try:
20  import Image
21  import PngImagePlugin
22except ImportError:
23  print "This script requires the Python Imaging Library to be installed."
24  sys.exit(1)
25frames = [Image.open(fn).convert("RGB"for fn in sys.argv[1:-1]]
26assert len(frames) > 0"Must have at least one input frame."
27sizes = set()
28for fr in frames:
29  sizes.add(fr.size)
30assert len(sizes) == 1"All input images must have the same size."
31w, h = sizes.pop()
32N = len(frames)
33out = Image.new("RGB", (w, h*N))
34for j in range(h):
35  for i in range(w):
36    for fn, f in enumerate(frames):
37      out.putpixel((i, j*N+fn), f.getpixel((i, j)))
38# When loading this image, the graphics library expects to find a text
39# chunk that specifies how many frames this animation represents.  If
40# you post-process the output of this script with some kind of
41# optimizer tool (eg pngcrush or zopflipng) make sure that your
42# optimizer preserves this text chunk.
43meta = PngImagePlugin.PngInfo()
44meta.add_text("Frames", str(N))
45out.save(sys.argv[-1], pnginfo=meta)

    这也就是为什么,调用这张图片需要用到LoadBitmapArray这个函数的原因。

1void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
2    int result = res_create_multi_display_surface(filename, frames, surface);
3    if (result < 0) {
4        LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
5    }
6}

调完这个函数后会调用

resources.cpp中的res_create_multi_display_surface函数用于显示,源码如下:

1int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) {
2    GRSurface** surface = NULL;
3    int result = 0;
4    png_structp png_ptr = NULL;
5    png_infop info_ptr = NULL;
6    png_uint_32 width, height;
7    png_byte channels;
8    int i;
9    png_textp text;
10    int num_text;
11    unsigned char* p_row;
12    unsigned int y;
13
14    *pSurface = NULL;
15    *frames = -1;
16
17    result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
18    if (result < 0return result;
19
20    *frames = 1;
21    if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
22        for (i = 0; i < num_text; ++i) {
23            if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
24                *frames = atoi(text[i].text);
25                break;
26            }
27        }
28        printf("  found frames = %d\n", *frames);
29    }
30
31    if (height % *frames != 0) {
32        printf("bad height (%d) for frame count (%d)\n", height, *frames);
33        result = -9;
34        goto exit;
35    }
36
37    surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*)));
38    if (surface == NULL) {
39        result = -8;
40        goto exit;
41    }
42    for (i = 0; i < *frames; ++i) {
43        surface[i] = init_display_surface(width, height / *frames);
44        if (surface[i] == NULL) {
45            result = -8;
46            goto exit;
47        }
48    }
49
50#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
51    png_set_bgr(png_ptr);
52#endif
53
54    p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
55    for (y = 0; y < height; ++y) {
56        png_read_row(png_ptr, p_row, NULL);
57        int frame = y % *frames;
58        unsigned char* out_row = surface[frame]->data +
59            (y / *frames) * surface[frame]->row_bytes;
60        transform_rgb_to_draw(p_row, out_row, channels, width);
61    }
62    free(p_row);
63
64    *pSurface = reinterpret_cast<GRSurface**>(surface);
65
66exit:
67    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
68
69    if (result < 0) {
70        if (surface) {
71            for (i = 0; i < *frames; ++i) {
72                if (surface[i]) free(surface[i]);
73            }
74            free(surface);
75        }
76    }
77    return result;
78}

其余的和text无关图片,会用到LoadBitmap这个函数:

1void ScreenRecoveryUI::LoadBitmapArray(const char* filename, int* frames, gr_surface** surface) {
2    int result = res_create_multi_display_surface(filename, frames, surface);
3    if (result < 0) {
4        LOGE("missing bitmap %s\n(Code %d)\n", filename, result);
5    }
6}

同样调用到以下函数:

1int res_create_multi_display_surface(const char* name, int* frames, GRSurface*** pSurface) {
2    GRSurface** surface = NULL;
3    int result = 0;
4    png_structp png_ptr = NULL;
5    png_infop info_ptr = NULL;
6    png_uint_32 width, height;
7    png_byte channels;
8    int i;
9    png_textp text;
10    int num_text;
11    unsigned char* p_row;
12    unsigned int y;
13
14    *pSurface = NULL;
15    *frames = -1;
16
17    result = open_png(name, &png_ptr, &info_ptr, &width, &height, &channels);
18    if (result < 0return result;
19
20    *frames = 1;
21    if (png_get_text(png_ptr, info_ptr, &text, &num_text)) {
22        for (i = 0; i < num_text; ++i) {
23            if (text[i].key && strcmp(text[i].key, "Frames") == 0 && text[i].text) {
24                *frames = atoi(text[i].text);
25                break;
26            }
27        }
28        printf("  found frames = %d\n", *frames);
29    }
30
31    if (height % *frames != 0) {
32        printf("bad height (%d) for frame count (%d)\n", height, *frames);
33        result = -9;
34        goto exit;
35    }
36
37    surface = reinterpret_cast<GRSurface**>(malloc(*frames * sizeof(GRSurface*)));
38    if (surface == NULL) {
39        result = -8;
40        goto exit;
41    }
42    for (i = 0; i < *frames; ++i) {
43        surface[i] = init_display_surface(width, height / *frames);
44        if (surface[i] == NULL) {
45            result = -8;
46            goto exit;
47        }
48    }
49
50#if defined(RECOVERY_ABGR) || defined(RECOVERY_BGRA)
51    png_set_bgr(png_ptr);
52#endif
53
54    p_row = reinterpret_cast<unsigned char*>(malloc(width * 4));
55    for (y = 0; y < height; ++y) {
56        png_read_row(png_ptr, p_row, NULL);
57        int frame = y % *frames;
58        unsigned char* out_row = surface[frame]->data +
59            (y / *frames) * surface[frame]->row_bytes;
60        transform_rgb_to_draw(p_row, out_row, channels, width);
61    }
62    free(p_row);
63
64    *pSurface = reinterpret_cast<GRSurface**>(surface);
65
66exit:
67    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
68
69    if (result < 0) {
70        if (surface) {
71            for (i = 0; i < *frames; ++i) {
72                if (surface[i]) free(surface[i]);
73            }
74            free(surface);
75        }
76    }
77    return result;
78}

    关于图片我们大概都知道怎么来显示的了,所以,现在我们可以替换Android原生态中的图片,换成我们自己的图片,当然,也不是什么图都可以的,在recovery中,所有的png图片必须是RGB且不带且不能带alhpa通道信息。关于这一点,我们可以看open_png这个函数:

 1static int open_png(const char* name, png_structp* png_ptr, png_infop* info_ptr,
2                    png_uint_32* width, png_uint_32* height, png_byte* channels)
 
{
3    char resPath[256];
4    unsigned char header[8];
5    int result = 0;
6    int color_type, bit_depth;
7    size_t bytesRead;
8
9    snprintf(resPath, sizeof(resPath)-1"/res/images/%s.png", name);
10    resPath[sizeof(resPath)-1] = '\0';
11    FILE* fp = fopen(resPath, "rb");
12    if (fp == NULL) {
13        result = -1;
14        goto exit;
15    }
16
17    bytesRead = fread(header, 1sizeof(header), fp);
18    if (bytesRead != sizeof(header)) {
19        result = -2;
20        goto exit;
21    }
22
23    if (png_sig_cmp(header, 0sizeof(header))) {
24        result = -3;
25        goto exit;
26    }
27
28    *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULLNULLNULL);
29    if (!*png_ptr) {
30        result = -4;
31        goto exit;
32    }
33
34    *info_ptr = png_create_info_struct(*png_ptr);
35    if (!*info_ptr) {
36        result = -5;
37        goto exit;
38    }
39
40    if (setjmp(png_jmpbuf(*png_ptr))) {
41        result = -6;
42        goto exit;
43    }
44
45    png_init_io(*png_ptr, fp);
46    png_set_sig_bytes(*png_ptr, sizeof(header));
47    png_read_info(*png_ptr, *info_ptr);
48
49    png_get_IHDR(*png_ptr, *info_ptr, width, height, &bit_depth,
50            &color_type, NULLNULLNULL);
51
52    *channels = png_get_channels(*png_ptr, *info_ptr);
53
54    if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
55        // 8-bit RGB images: great, nothing to do.
56    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
57        // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
58        png_set_expand_gray_1_2_4_to_8(*png_ptr);
59    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
60        // paletted images: expand to 8-bit RGB.  Note that we DON'T
61        // currently expand the tRNS chunk (if any) to an alpha
62        // channel, because minui doesn't support alpha channels in
63        // general.
64        png_set_palette_to_rgb(*png_ptr);
65        *channels = 3;
66    } else {
67        fprintf(stderr"minui doesn't support PNG depth %d channels %d color_type %d\n",
68                bit_depth, *channels, color_type);
69        result = -7;
70        goto exit;
71    }
72
73    return result;
74
75  exit:
76    if (result < 0) {
77        png_destroy_read_struct(png_ptr, info_ptr, NULL);
78    }
79    if (fp != NULL) {
80        fclose(fp);
81    }
82
83    return result;
84}

在代码中,我们可以看到如下:

1 if (bit_depth == 8 && *channels == 3 && color_type == PNG_COLOR_TYPE_RGB) {
2        // 8-bit RGB images: great, nothing to do.
3    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_GRAY) {
4        // 1-, 2-, 4-, or 8-bit gray images: expand to 8-bit gray.
5        png_set_expand_gray_1_2_4_to_8(*png_ptr);
6    } else if (bit_depth <= 8 && *channels == 1 && color_type == PNG_COLOR_TYPE_PALETTE) {
7        // paletted images: expand to 8-bit RGB.  Note that we DON'T
8        // currently expand the tRNS chunk (if any) to an alpha
9        // channel, because minui doesn't support alpha channels in
10        // general.
11        png_set_palette_to_rgb(*png_ptr);
12        *channels = 3;
13    } else {
14        fprintf(stderr"minui doesn't support PNG depth %d channels %d color_type %d\n",
15                bit_depth, *channels, color_type);
16        result = -7;
17        goto exit;
18    }

以下参考一位网友给出的解决方案。

    这个函数将图片文件的数据读取到内存,我在其中输出了一些调试信息,输出图片的 color_type, channels 等信息。查看LOG发现,android原生的图片 channels == 3,channels 即色彩通道个数,等于 3 的话,意味着只有 R,G,B 三个通道的信息,没有 ALPHA 通道信息!这段代码的逻辑是如果channels 不等于3, 则按channels = 1 来处理,即灰度图。美工给的图片是带 alpha通道信息的,即channels = 4,被当成灰度图像来处理了,怪不得显示的效果是灰度图像。我一直以为 png 图像就只有一种格式,都是带有 alpha通道的。。。使用图像处理工具(photoshop 或者 gimp),将美工给的图片去掉 alpha 通道信息,再替换recovery 的图片,编译,替换recovery.img ,reboot -r 。图片终于正常显示啦。

韦东山老师课程推荐








商务合作

支持我请给我在看!


免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

罗德与施瓦茨与SmartViser携手开发了一种用于测试符合欧盟销售的智能手机和平板电脑的新Energy Efficiency Index(EEI)标签法规的解决方案。该解决方案的核心是R&S CMX500,这是...

关键字: 智能手机 Android iOS

(全球TMT2023年8月24日讯)2023年8月23日,时值实时3D引擎Unity在华设立合资公司Unity中国一周年之际,Unity中国正式推出Unity中国版引擎——团结引擎。Unity全球CEO John Ri...

关键字: UNITY CE Android 开发者

报告显示:全球电商 App 获客花费接近50亿美元 北京2023年8月23日 /美通社/ -- 全球营销衡量与体验管理平台 AppsFlyer 近日发布《2023 电商 App 营销现状报告》。尽管面临全球经...

关键字: APPS BSP iOS Android

数字机顶盒是一种数字技术下的多媒体娱乐中心,可以实现电视节目接收、播放、存储、网络应用等多种功能。随着科技的发展,数字机顶盒的设计方案也在不断进步和优化。本文将介绍数字机顶盒设计的几种实现方案。

关键字: 数字机顶盒 Android Linux

21ic 近日获悉,原小米 9 号创始员工李明在社交媒体平台公布了旗下首款产品乐天派桌面机器人,为全球首款 Android 桌面机器人,面向极客和发烧友的 AI + 机器人。据悉,李明两个月前宣布创业并进军 AI 领域,...

关键字: 小米 Android 桌面机器人 AI

尽管安装增长放缓,全球游戏 App 获客花费仍高达 267 亿美元 经济低迷导致 2023 游戏 App 营销优先考虑收入指标,用户增长次之 北京2023年3月9日 /美通社/ -- 今天,全球营销衡量与体验管理平台...

关键字: APPS iOS Android BSP

量子计算领域的新里程碑,来了! 谷歌科学家证明,通过增加量子比特的数量,就能降低量子计算的错误率。

关键字: 谷歌 Android Windows

「卫星通讯」正在被普及到每一台智能手机当中。普及的动机并非是消费市场的一个刚需,其实更像是将差异化的功能「抹平」成一个标配。时下,支持「卫星通讯」功能的智能手机只有苹果的 iPhone 14 系列与华为的 Mate 50...

关键字: 卫星通讯 Android 智能手机 iPhone

Android是Google开发的操作系统,支持多种指令集架构 (ISA),包括Arm和x86,多数使用Android的设备都采用Arm架构芯片组。新兴RISC-V架构是免费开放指令集架构,任何人都可用它设计芯片,且无需...

关键字: 谷歌 Android RISC-V架构

智能手机并非每年都取得重大进展,这导致越来越多的人将手机保留两年、三年或四年。不过,普通的 Android 手机能否在遇到问题之前使用那么久?

关键字: Android 安卓 谷歌 智能手机
关闭
关闭