java怎么处理黑白bmp_Android JNI处理图片实现黑白滤镜的方法
前言
在Android的开发中,我们有时会遇到对性能要求比较高的模块。所幸Android通过NDK为我们提供了c++开发的方式。我们可以通过c++完成核心的耗时的计算,然后通过JNI的方式将处理完成的数据传给Java层。
今天,我们就从一个很小的角度(Bitmap)的处理,来实践NDK开发的方式。开发一个小小的图片滤镜。
准备
新版本的Android Studio在新建工程时,就可以选择Include C++ support
当我们勾上这个选择后,Android Studio就会帮我们自动完成,c++开发目录的创建。
我们先看一下CMakeLists.txt:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib jnigraphics
# Links the target library to the log library
# included in the NDK.
${log-lib} )
我们可以看到,这个文件中,包含了我们需要使用的cpp库和cpp文件。由于这一次的例子,我们需要开发Bitmap相关的功能,所以我加入了jnigraphics。
Java
先看代码:
public class MainActivity extends AppCompatActivity {
private ImageView mImg1, mImg2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImg1 = (ImageView) findViewById(R.id.img_test1_id);
mImg2 = (ImageView) findViewById(R.id.img_test2_id);
}
/**
* 确定native处理图片的接口
* @param bitmap 需要被处理的图片
*/
public native void nativeProcessBitmap(Bitmap bitmap);
/**
* 引入native库
*/
static {
System.loadLibrary("native-lib");
}
/**
* 点击开始加载图片
* @param view
*/
public void onLoadClick(View view) {
Bitmap originalBitmap = loadBitmap();
mImg1.setImageBitmap(originalBitmap);
Bitmap resultBitmap = processBitmap(originalBitmap);
mImg2.setImageBitmap(resultBitmap);
}
/**
* 从assets中加载图片
* @return
*/
private Bitmap loadBitmap() {
Bitmap bmp = null;
AssetManager am = getResources().getAssets();
try {
InputStream is = am.open("test_img.jpg");
BitmapFactory.Options options = new BitmapFactory.Options();
bmp = BitmapFactory.decodeStream(is ,null , options);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bmp;
}
/**
* 处理图片,此方法中会调用nativeProcessBitmap
* @param bitmap
* @return
*/
private Bitmap processBitmap(Bitmap bitmap) {
Bitmap bmp = bitmap.copy(Bitmap.Config.ARGB_8888, true);
nativeProcessBitmap( bmp);
return bmp;
}
}
代码比较简单。不作过多的解释。
与图片相关的事情只有两件:
引入native-lib库
确定了native接口:public native void nativeProcessBitmap(Bitmap bitmap);
其他的代码都是为了demo效果写的一些业务代码,不作过多赘述。
C++
由于c++的代码比较长,我们分段来看。
#include
#include
#include
#include
#ifndef eprintf
#define eprintf(...) __android_log_print(ANDROID_LOG_ERROR,"@",__VA_ARGS__)
#endif
这一段主要引入了我们需要的库并宏定义了eprintf,方便我们打日志并进行调试。
#define MAKE_RGB565(r,g,b) ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
#define MAKE_ARGB(a,r,g,b) ((a&0xff)<<24) | ((r&0xff)<<16) | ((g&0xff)<<8) | (b&0xff)
#define RGB565_R(p) ((((p) & 0xF800) >> 11) << 3)
#define RGB565_G(p) ((((p) & 0x7E0 ) >> 5) << 2)
#define RGB565_B(p) ( ((p) & 0x1F ) << 3)
#define RGB8888_A(p) (p & (0xff<<24) >> 24 )
#define RGB8888_R(p) (p & (0xff<<16) >> 16 )
#define RGB8888_G(p) (p & (0xff<<8) >> 8 )
#define RGB8888_B(p) (p & (0xff) )
这一段定义了RGB565和ARGB8888的读写方法。对于RGB565和ARGB8888格式不熟悉的同学,可以参考:
在Android的Bitmap.Config中有四个枚举类型:ALPHA_8、ARGB_4444、ARGB_8888和RGB_565
下面是这四种类型的详细解释:
ALPHA_8:每个像素都需要1(8位)个字节的内存,只存储位图的透明度,没有颜色信息
ARGB_4444:A(Alpha)占4位的精度,R(Red)占4位的精度,G(Green)占4位的精度,B(Blue)占4位的精度,加起来一共是16位的精度,折合是2个字节,也就是一个像素占两个字节的内存,同时存储位图的透明度和颜色信息。不过由于该精度的位图质量较差,官方不推荐使用
ARGB_8888:这个类型的跟ARGB_4444的原理是一样的,只是A,R,G,B各占8个位的精度,所以一个像素占4个字节的内存。由于该类型的位图质量较好,官方特别推荐使用。但是,如果一个480*800的位图设置了此类型,那个它占用的内存空间是:480*800*4/(1024*1024)=1.5M
RGB_565:同理,R占5位精度,G占6位精度,B占5位精度,一共是16位精度,折合两个字节。这里注意的时,这个类型存储的只是颜色信息,没有透明度信息
值得注意的是虽然RGB565的三色只有5位信息,但其实它们的值是8位,提供的5位信息是高5位的信息。
extern "C"
{
JNIEXPORT void JNICALL
Java_com_live_longsiyang_jnibitmapdemo_MainActivity_nativeProcessBitmap(JNIEnv *env,
jobject instance,
jobject bitmap) {
if (bitmap == NULL) {
eprintf("bitmap is null\n");
return;
}
AndroidBitmapInfo bitmapInfo;
memset(&bitmapInfo , 0 , sizeof(bitmapInfo));
// Need add "jnigraphics" into target_link_libraries in CMakeLists.txt
AndroidBitmap_getInfo(env , bitmap , &bitmapInfo);
// Lock the bitmap to get the buffer
void * pixels = NULL;
int res = AndroidBitmap_lockPixels(env, bitmap, &pixels);
// From top to bottom
int x = 0, y = 0;
for (y = 0; y < bitmapInfo.height; ++y) {
// From left to right
for (x = 0; x < bitmapInfo.width; ++x) {
int a = 0, r = 0, g = 0, b = 0;
void *pixel = NULL;
// Get each pixel by format
if(bitmapInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888)
{
pixel = ((uint32_t *)pixels) + y * bitmapInfo.width + x;
int r,g,b;
uint32_t v = *((uint32_t *)pixel);
r = RGB8888_R(v);
g = RGB8888_G(v);
b = RGB8888_B(v);
int sum = r+g+b;
*((uint32_t *)pixel) = MAKE_ARGB(0x1f , sum/3, sum/3, sum/3);
}
else if (bitmapInfo.format == ANDROID_BITMAP_FORMAT_RGB_565) {
pixel = ((uint16_t *)pixels) + y * bitmapInfo.width + x;
int r,g,b;
uint16_t v = *((uint16_t *)pixel);
r = RGB565_R(v);
g = RGB565_G(v);
b = RGB565_B(v);
int sum = r+g+b;
*((uint16_t *)pixel) = MAKE_RGB565(sum/3, sum/3, sum/3); }
}
}
AndroidBitmap_unlockPixels(env, bitmap);
}
}
这一段代码虽然长,但逻辑其实非常简单。
AndroidBitmapInfo bitmapInfo;
memset(&bitmapInfo , 0 , sizeof(bitmapInfo));
// Need add "jnigraphics" into target_link_libraries in CMakeLists.txt
AndroidBitmap_getInfo(env , bitmap , &bitmapInfo);
我们通过bitmap获得AndroidBitmapInfo对象。AndroidBitmapInfo为我们提供了Bitmap的所有信息。
然后我们,再调用AndroidBitmap_lockPixels:
void * pixels = NULL;
int res = AndroidBitmap_lockPixels(env, bitmap, &pixels);
获得bitmap的像素矩阵,并将它存放在&pixels中。
pixels的每一位就包含了一个像素点的颜色信息。因此在RGB565模式下,它就是16位的,在ARGB8888模式下,它就是24位的。最后,我对RGB三色的值取了平均,从而得到一个新的图片。在这个图片中,RGB三色的值是相等的。因此,它是一个黑白图片。
我们在修改图片的像素值时,图片其实是被锁定的,修改完成后,我们需要解锁:
AndroidBitmap_unlockPixels(env, bitmap);
至此,我们的图片修改就完成了。最后看一下效果。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
java怎么处理黑白bmp_Android JNI处理图片实现黑白滤镜的方法相关推荐
- android jni java调用c,Android与JNI(一) ---- Java调用C 静态调用
第一.通过eclipse新建一个工程名为HelloJni的android工程,并编译. 第二.右键工程-->Android Tools --> Add Native Support,出现如 ...
- JNI开发笔记(八)--Java读取txt文件进行JNI测试
Java读取txt文件进行JNI测试 引 前言 1. 新建assets文件夹 2. 载入测试文件 3. 建立文件读取方法 4. 在MainActivity中读取文件数据 引 JNI开发笔记(一)–An ...
- ubuntu20.04 java 开发环境搭建及 JNI 使用
java开发环境搭建及JNI使用 本文采用知识共享署名 4.0 国际许可协议进行许可,转载时请注明原文链接,图片在使用时请保留全部内容,可适当缩放并在引用处附上图片所在的文章链接. 1. VSCODE ...
- 使用java命令出现Error: A JNI error has occurred
使用java命令出现Error: A JNI error has occurred, please check your installation and try again的错误 运行javac H ...
- JAVA字符串前补零和后补零的快速方法
JAVA字符串前补零和后补零的快速方法 String fileName = "130181";System.out.println("================ 前 ...
- java poll()是什么方法,JAVA学习中的一点小发现:LinkedList中.poll()方法在输出时使用应注意的小点...
在学习JAVA类集一章中关于LinkedList子类中.poll()方法易错点的分析心得 例如如下代码: package Test; import java.util.LinkedList; publ ...
- linux没有jre文件夹,linux上配置jdk时,java命令提示没有此文件或文件夹的解决方法...
linux上配置jdk时,java命令提示没有此文件或文件夹的解决方法 出现这个问题可能有以下几种原因: 1.对该文件没有执行的权限. 2.我们的机器是64位的,而下载的jdk是32位的. 我就是后一 ...
- Java 8新特性探究(二)深入解析默认方法
转载自 Java 8新特性探究(二)深入解析默认方法 什么是默认方法,为什么要有默认方法 简单说,就是接口可以有实现方法,而且不需要实现类去实现其方法.只需在方法名前面加个default关键字即可. ...
- java泛型方法 通配符_Java泛型教程–示例类,接口,方法,通配符等
java泛型方法 通配符 泛型是Java编程的核心功能之一,它是Java 5中引入的.如果您使用的是Java Collections ,并且版本5或更高版本,则可以肯定使用了它. 将泛型与集合类一起使 ...
最新文章
- 【高级数据结构】并查集
- POJ1325二分匹配或者DINIC(最小路径覆盖)
- c#sdf数据库连接_如何连接并处理 sdf 数据库文件(便捷数据库处理)
- 循环队列真的没那么难,就那么几个注意点,附Java代码及运行效果
- js基础语法(01)-JS中+号的三种含义
- CodeForces 731C Socks
- css 浮动和清除浮动
- Scala.js:将Scala编译为JavaScript的编译器
- java smp_什么是SMP系统
- jQuery学习之三---工具
- python纵向制表符命令_Python中raw_input()中子命令的制表符完成
- Tab选项卡界面(1)
- 测试自己幸运数字的软件,心理测试:4个数字哪个是你的幸运数字?测试你的心理年龄!...
- 图书管理系统软件测试说明,图书管理系统软件测试报告
- 线性代数学习笔记8-3:二次型、合同矩阵、标准型、规范型
- 3万字报告拆解:硅谷寒冬,投资人还在哪些赛道烧钱?|我在硅谷看未来
- Warshall‘s algorithm 算法的实现及优化(修改版)
- uniapp中输入框禁用表情与特殊符号
- CAD绘图设计中怎样删除CAD图层?怎样清理CAD图层文件?
- Robust stochastic frontier analysis
热门文章
- 河南高考成绩位次查询2021,2021年河南高考状元多少分是谁,河南高考状元名单资料...
- 蓝牙解码格式哪个最好_让有源音箱飞起来,让汽车音响硬起来,飞傲BTR5蓝牙HiFi解码品评...
- java中list空指针异常,List空指针异常
- PDF修改目录和跳转
- Hive 优化(通用版)
- 如何使用Windows Live Writer远程发布到WordPress
- so链接及动态加载原理分析
- 垃圾回收器判断对象是否存活
- SQL 四大功能DDL/DML/DCL/TCL
- 各类国外软件国内下载镜像合集