当前位置:   article > 正文

Android使用libjpeg实现图片压缩——非常具有参考意义_libjpeg 下采样

libjpeg 下采样

http://blog.csdn.net/a992036795/article/details/53814178


一、Android中使用的图片压缩库 
androidiOS 中图片处理使用了一个叫做skia的开源图形处理引擎。他位于android源码的/external/skia 目录。我们平时在Java层使用一个图片处理的函数实际上底层就是调用了这个开源引擎中的相关的函数。 
二、Android 中常用的压缩方式 
Android中常用压缩方法分为2种:一种是降采样率压缩,另外一种是质量压缩。 
代码: 
1.降采样率压缩的一般写法:

  1. public static Bitmap obtainImageFromPath(String path, int width, int height) {
  2. BitmapFactory.Options o = new BitmapFactory.Options();
  3. o.inJustDecodeBounds = true;
  4. BitmapFactory.decodeFile(path, o);
  5. o.inSampleSize = calculateSampleSize(o, width, height);
  6. o.inJustDecodeBounds = false;
  7. return BitmapFactory.decodeFile(path, o);
  8. }
  9. private static int calculateSampleSize(BitmapFactory.Options o, int reqWidth, int reqHeight) {
  10. int sampleSize = 1;
  11. if (o.outWidth > reqWidth || o.outHeight > reqHeight) {
  12. final int halfWidth = o.outWidth / 2;
  13. final int halfHeight = o.outHeight / 2;
  14. while ((halfHeight / sampleSize) >= reqHeight
  15. && (halfWidth / sampleSize) >= reqWidth) {
  16. sampleSize *= 2;
  17. }
  18. }
  19. return sampleSize;
  20. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

2.质量压缩的一般写法:

bitmap.compress(Bitmap.CompressFormat.JPEG, 20, new FileOutputStream("sdcard/result.jpg"));
 
 
  • 1
  • 1

三、libjpeg 
我们使用质量压缩的话它的底层就是用skia引擎进行处理,加入我们调用bitmap.compress(Bitmap.CompressFormat.JPEG,…..) 他实际会 使用一个libjpeg.so 的动态库进行编码压缩。 
android在进行jpeg压缩编码的时候,考虑到了效率问题使用了定长编码方式进行编码(因为当时的手机性能都比较低),而ios使用了变长编码的算法——哈夫曼算法。而且IOS对skia引擎也做了优化。所有我们看到同样的图片在ios上压缩会好一点。

四、优化思路 
上文我们知道之所以在android机进行质量压缩没有IOS上压缩好的原因,那么我们也就应该有了相应的优化思路。我们的思路如下: 
1、下载开源的libjpeg,进行移植、编译得到libjpeg.so 
2、使用jni编写一个函数用来图片压缩 
3、在函数中添加一个开关选项,可以让我们选择是否使用哈夫曼算法。 
4、打包,搞成sdk供我们以后使用。

五、实现 
1、下载libjpeg 编译 
使用Git clone最新的android分支

git clone git://git.linaro.org/people/tomgall/libjpeg-turbo/libjpeg-turbo.git -b linaro-android

 
 
  • 1
  • 2
  • 1
  • 2

2、编译 
记得配置好ndk,设置环境变量

  1. 1、把文件名变成 jni
  2. mv libjpeg-turbo jni
  3. 2、编译
  4. ndk-build APP_ABI=armeabi-v7a,armeabi
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

3、使用AndroidStudio创建一个项目,记得勾选c++ support

4、加入编译好的 动态库,和头文件并且在在配置文件中引用

5、编写代码:

  1. #include "compress.h"
  2. #include "lang.h"
  3. #include <android/bitmap.h>
  4. #include <setjmp.h>
  5. #include <jpeglib.h>
  6. #include <stdlib.h>
  7. #define true 1
  8. #define false 0
  9. typedef u_int8_t BYTE;
  10. struct my_error_mgr {
  11. struct jpeg_error_mgr pub;
  12. jmp_buf setjmp_buffer;
  13. };
  14. typedef struct my_error_mgr *my_error_ptr;
  15. METHODDEF(void)
  16. my_error_exit(j_common_ptr
  17. cinfo) {
  18. my_error_ptr myerr = (my_error_ptr) cinfo->err;
  19. (*cinfo->err->output_message)(cinfo);
  20. LOGW("jpeg_message_table[%d]:%s",
  21. myerr->pub.msg_code, myerr->pub.jpeg_message_table[myerr->pub.msg_code]);
  22. longjmp(myerr
  23. ->setjmp_buffer, 1);
  24. }
  25. int generateJPEG(BYTE *data, int w, int h, jint quality, const char *name, boolean optimize);
  26. const char *jstringToString(JNIEnv *env, jstring jstr);
  27. JNIEXPORT jint
  28. JNICALL
  29. Java_com_blueberry_compress_ImageCompress_nativeCompressBitmap(JNIEnv *env, jclass type,
  30. jobject bitmap, jint quality,
  31. jstring dstFile_,
  32. jboolean optimize) {
  33. AndroidBitmapInfo androidBitmapInfo;
  34. BYTE *pixelsColor;
  35. int ret;
  36. BYTE *data;
  37. BYTE *tmpData;
  38. const char *dstFileName = jstringToString(env, dstFile_);
  39. //解码Android Bitmap信息
  40. if ((ret = AndroidBitmap_getInfo(env, bitmap, &androidBitmapInfo)) < 0) {
  41. LOGD("AndroidBitmap_getInfo() failed error=%d", ret);
  42. return ret;
  43. }
  44. if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixelsColor)) < 0) {
  45. LOGD("AndroidBitmap_lockPixels() failed error=%d", ret);
  46. return ret;
  47. }
  48. LOGD("bitmap: width=%d,height=%d,size=%d , format=%d ",
  49. androidBitmapInfo.width, androidBitmapInfo.height,
  50. androidBitmapInfo.height * androidBitmapInfo.width,
  51. androidBitmapInfo.format);
  52. BYTE r, g, b;
  53. int color;
  54. int w, h, format;
  55. w = androidBitmapInfo.width;
  56. h = androidBitmapInfo.height;
  57. format = androidBitmapInfo.format;
  58. data = (BYTE *) malloc(androidBitmapInfo.width * androidBitmapInfo.height * 3);
  59. tmpData = data;
  60. // 将bitmap转换为rgb数据
  61. for (int i = 0; i < h; ++i) {
  62. for (int j = 0; j < w; ++j) {
  63. //只处理 RGBA_8888
  64. if (format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
  65. color = (*(int *) (pixelsColor));
  66. // 这里取到的颜色对应的 A B G R 各占8位
  67. b = (color >> 16) & 0xFF;
  68. g = (color >> 8) & 0xFF;
  69. r = (color >> 0) & 0xFF;
  70. *data = r;
  71. *(data + 1) = g;
  72. *(data + 2) = b;
  73. data += 3;
  74. pixelsColor += 4;
  75. } else {
  76. return -2;
  77. }
  78. }
  79. }
  80. AndroidBitmap_unlockPixels(env, bitmap);
  81. //进行压缩
  82. ret = generateJPEG(tmpData, w, h, quality, dstFileName, optimize);
  83. free((void *) dstFileName);
  84. free((void *) tmpData);
  85. return ret;
  86. }
  87. int generateJPEG(BYTE *data, int w, int h, int quality, const char *name, boolean optimize) {
  88. int nComponent = 3;
  89. struct jpeg_compress_struct jcs;
  90. //自定义的error
  91. struct my_error_mgr jem;
  92. jcs.err = jpeg_std_error(&jem.pub);
  93. jem.pub.error_exit = my_error_exit;
  94. if (setjmp(jem.setjmp_buffer)) {
  95. return 0;
  96. }
  97. //为JPEG对象分配空间并初始化
  98. jpeg_create_compress(&jcs);
  99. //获取文件信息
  100. FILE *f = fopen(name, "wb");
  101. if (f == NULL) {
  102. return 0;
  103. }
  104. //指定压缩数据源
  105. jpeg_stdio_dest(&jcs, f);
  106. jcs.image_width = w;
  107. jcs.image_height = h;
  108. jcs.arith_code = false;
  109. jcs.input_components = nComponent;
  110. jcs.in_color_space = JCS_RGB;
  111. jpeg_set_defaults(&jcs);
  112. jcs.optimize_coding = optimize;
  113. //为压缩设定参数,包括图像大小,颜色空间
  114. jpeg_set_quality(&jcs, quality, true);
  115. //开始压缩
  116. jpeg_start_compress(&jcs, true);
  117. JSAMPROW row_point[1];
  118. int row_stride;
  119. row_stride = jcs.image_width * nComponent;
  120. while (jcs.next_scanline < jcs.image_height) {
  121. row_point[0] = &data[jcs.next_scanline * row_stride];
  122. jpeg_write_scanlines(&jcs, row_point, 1);
  123. }
  124. if (jcs.optimize_coding) {
  125. LOGI("使用了哈夫曼算法完成压缩");
  126. } else {
  127. LOGI("未使用哈夫曼算法");
  128. }
  129. //压缩完毕
  130. jpeg_finish_compress(&jcs);
  131. //释放资源
  132. jpeg_destroy_compress(&jcs);
  133. fclose(f);
  134. return 1;
  135. }
  136. const char *jstringToString(JNIEnv *env, jstring jstr) {
  137. char *ret;
  138. const char *tempStr = (*env)->GetStringUTFChars(env, jstr, NULL);
  139. jsize len = (*env)->GetStringUTFLength(env, jstr);
  140. if (len > 0) {
  141. ret = (char *) malloc(len + 1);
  142. memcpy(ret, tempStr, len);
  143. ret[len] = 0;
  144. }
  145. (*env)->ReleaseStringUTFChars(env, jstr, tempStr);
  146. return ret;
  147. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 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
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 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
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180

6、最后测试

我测试发现确实有些改善,使用同样的压缩等级,采用哈夫曼算法的话,会压缩的更小一些。

7、最后 
代码地址: 
https://github.com/blueberryCoder/Compress







声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/140554
推荐阅读
相关标签
  

闽ICP备14008679号