浅谈NDK修改Bitmap内容

   
 
跳转到指定楼层
楼主




Android开发中,Bitmap该类的使用频繁度大家都应该都知道。
基本可以说是到了每个App必备的地步。

但是我经常会碰到一个问题。就是在频繁的更改Bitmap内容的时候,如果总是创建,回收,肯定会造成性能上的浪费以及不必要的OOM。

对于这类问题,我很喜欢将Bitmap内容的修改 丢到NDK下去做。

而且对于Bitmap内容的修改,必然涉及到像素点的操作,而这种操作在NDK下完成也比在JAVA端效率的多。


Android platforms\android-8\arch-arm\usr\include\android 目录下提供了Bitmap.h 文件

其中包括了:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
  * Given a java bitmap object, fill out the AndroidBitmap struct for it.
  * If the call fails, the info parameter will be ignored
  */
int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
                           AndroidBitmapInfo* info);
 
/**
  * Given a java bitmap object, attempt to lock the pixel address.
  * Locking will ensure that the memory for the pixels will not move
  * until the unlockPixels call, and ensure that, if the pixels had been
  * previously purged, they will have been restored.
  *
  * If this call succeeds, it must be balanced by a call to
  * AndroidBitmap_unlockPixels, after which time the address of the pixels should
  * no longer be used.
  *
  * If this succeeds, *addrPtr will be set to the pixel address. If the call
  * fails, addrPtr will be ignored.
  */
int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void ** addrPtr);
 
/**
  * Call this to balanace a successful call to AndroidBitmap_lockPixels
  */
int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);



这三个方法,看着方法名称都应该有所了解了。

下面我就以Jpeg数据到Bitmap为例子。

1、JNI部分的方法

首先我定义了个 JNI的一个宏。(每次写这么一长段,个人感觉很烦)。

?
代码片段,双击复制
01
#define  JNI_METHOD_JPEG(x)  Java_[PackageName]_[ClassName]_##x



接下来我定义了三个方法以及几个成员变量:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
typedef unsigned char * PCHAR ;
 
PCHAR j_p_src_buf;
PCHAR j_bmp_pixels;
jobject j_bitmap;
jbyteArray* j_src_buf;
 
 
void JNI_METHOD_JPEG(decompress)(JNIEnv *env, jobject thiz, jint offset,jint size);
 
jint JNI_METHOD_JPEG(create)(JNIEnv *env, jobject thiz, jbyteArray* src_buf,jobject bitmap);
 
jint JNI_METHOD_JPEG(release)(JNIEnv *env, jobject thiz);



具体实现为:

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// 解码
void JNI_METHOD_JPEG(decompress)(JNIEnv *env, jobject thiz, jint offset,jint size) {
 
// 每次修改像素时 ,必须先 lock 住bitmap ,修改完后再unlock.
// 当然该函数是有返回值的,表明是否lock成功。
         AndroidBitmap_lockPixels(env, j_bitmap, &j_bmp_pixels);
 
         JPEG_DECOMPRESS_MEM(rgba_8888)(j_p_src_buf + offset, size, j_bmp_pixels);
 
         AndroidBitmap_unlockPixels(env, j_bitmap);
}
 
 
// 创建
jint JNI_METHOD_JPEG(create)(JNIEnv *env, jobject thiz, jbyteArray* src_buf,
                jobject bmp) {
 
         j_bitmap = bmp;
 
         j_src_buf = src_buf;
 
         AndroidBitmapInfo bmp_info;
 
         j_p_src_buf = ( PCHAR ) ((*env)->GetByteArrayElements(env, j_src_buf, NULL));
 
         int check_ret = 0;
 
         if ((check_ret = AndroidBitmap_getInfo(env, j_bitmap, &bmp_info)) < 0) {
                LOGE( "check ret < 0" );
                return FALSE;
         }
 
         int width = bmp_info.width;
         int height = bmp_info.height;
         int format = bmp_info.format;
 
         LOGE( "JPEG Create Over!!! {Bitmap width[%d] , height[%d] , format[%d]}" , width,
                         height, format);
 
         return TRUE;
}
 
// 释放资源
jint JNI_METHOD_JPEG(release)(JNIEnv *env, jobject thiz)
{
         (*env)->ReleaseByteArrayElements(env, j_src_buf, j_p_src_buf, JNI_ABORT);
         return TRUE;
}



我当时的想法是,开一块固定的Buffer去接受Jpeg数据,再开一块固定的Bitmap去更改内容。
因此Create函数中已经将这两块Buffer地址传进来了。

在Decompress 函数中。只需要指定 偏移量和长度。

最后 全部完成 后 释放。

在Decompress函数中用了Libjpeg去解,

?
代码片段,双击复制
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
void JPEG_DECOMPRESS_MEM(rgba_8888)( PCHAR src_buf, int size, PCHAR dst_buf) {
 
         struct jpeg_decompress_struct cinfo;
         struct jpeg_error_mgr err;
 
         JSAMPARRAY buffer;
 
         cinfo.err = jpeg_std_error(&err);
         jpeg_create_decompress(&cinfo);
         jpeg_mem_src(&cinfo, src_buf, size);
         jpeg_read_header(&cinfo, TRUE);
         jpeg_start_decompress(&cinfo);
 
         int bmp_w = cinfo.output_width;
         int bmp_h = cinfo.output_height;
         int bmp_output_cmt = cinfo.output_components;
 
         int row_stride = bmp_w * bmp_output_cmt;
         int row_4_stride = bmp_w * 4;
 
//        LOGE("BMP -> ( w : %d , h : %d , cmt : %d) , size : %d", bmp_w, bmp_h,
//                        bmp_output_cmt, size);
 
         buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE,
                         row_stride, 1);
 
         int y = 0;
         PCHAR   p;
         while (cinfo.output_scanline < bmp_h) {
                jpeg_read_scanlines(&cinfo, buffer, 1);
                p = buffer[0];
                int w = 0;
                int i = 0;
 
                for (; w < row_4_stride; w += 4, i += 3) {
                         *(dst_buf + row_4_stride * y + w) = *(p + i);
                         *(dst_buf + row_4_stride * y + w + 1) = *(p + i + 1);
                         *(dst_buf + row_4_stride * y + w + 2) = *(p + i + 2);
                         *(dst_buf + row_4_stride * y + w + 3) = 0xff;
                }
 
                y++;
         }
 
         jpeg_finish_decompress(&cinfo);
         jpeg_destroy_decompress(&cinfo);
}



其中 dst_buf 为 Bitmap 中的像素点。 LibJpeg解码出来的是 RGB_888 (当然你也可以选择其他的)。

而不知道为什么 Android 下并没有  RGB_888 的 Bitmap ,因此我选择了 ARGB_8888 , (他们之间转换最简单了)

默认给 A 赋值 0xff 。(libjpeg 我测试了下 解码速度还行 320*240 大约10ms).

由于这里主要讲的是 Bitmap 因此 Libjpeg 在这不具体讨论,如有感兴趣的 可以单聊。

之后android 端调用就很简单了。。

?
代码片段,双击复制
01
02
03
04
05
06
07
08
public class EncodeUtils
{
       public static native void decompress( int start, int size);
 
       public static native int create( byte [] jpg_data, Bitmap bmp);
         
       public static native void release();
}



申明下,然后用的时候直接调用即可。

============================================================================

该帖子主要目的是为Bitmap 提供另一个修改舞台。 希望对Bitmap操作或者使用过程中,能有更方便,更便捷的方式。

android 采用java语言开发上层应用。而java本身就是面向对象的一个语言,所以在日常开发中,我们习惯了用一个个对象去处理。

而java的回收机制,有的时候也限制了我们对性能方面的发展。习惯了不停的new new new .虽然他能自动回收。但是有的时候关键的时候给你回收下,确实很卡。

C/C++中常用做法就是,开一块很大内存然后复用呗。 这样由于只有 创建一次,其他都是复用,因此在回收申请方面也提高了效率。(这个是我这帖子主要想讲的思想)。


Thanks.









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值