Demo链接:https://github.com/MrCoderDing/ImageFilterByColorMatrix
说到滤镜一般比较熟悉的是CIFilter、GPUImage、vImage API还有一种比较简单的滤镜ColorMatrix。
之前在项目中一个小功能要加滤镜,需求是要够用,方便。在没有考虑直接用第三方滤镜的情况下,最后还是选择使用ColorMatrix来完成任务。并且这个方案安卓和iOS通用,如果花时间做一套颜色矩阵,成本将降低很多。
1、颜色矩阵
lomo和黑白我做了细微调整,会稍微舒服一些:
const float colormatrix_lomo[] = {
// 1.7f, 0.1f, 0.1f, 0, -73.1f,
// 0, 1.7f, 0.1f, 0, -73.1f,
// 0, 0.1f, 1.6f, 0, -73.1f,
// 0, 0, 0, 1.0f, 0
1.20, 0.10, 0.10, 0.00, -73.10,
0.00, 1.20, 0.10, 0.00, -73.10,
0.00, 0.10, 1.10, 0.00, -73.10,
0.00, 0.00, 0.00, 0.00, 0.00
};
// 2、黑白
const float colormatrix_heibai[] = {
// 0.8f, 1.6f, 0.2f, 0, -163.9f,
// 0.8f, 1.6f, 0.2f, 0, -163.9f,
// 0.8f, 1.6f, 0.2f, 0, -163.9f,
// 0, 0, 0, 1.0f, 0
0.00, 0.00, 1.00, 0.00, -1,
0.00, 0.00, 1.00, 0.00, -1,
0.00, 0.00, 1.00, 0.00, -1,
0.00, 0.00, 0.00, 1.00, 0.00
};
// 3、复古
const float colormatrix_huajiu[] = {
0.2f,0.5f, 0.1f, 0, 40.8f,
0.2f, 0.5f, 0.1f, 0, 40.8f,
0.2f,0.5f, 0.1f, 0, 40.8f,
0, 0, 0, 1, 0 };
// 4、哥特
const float colormatrix_gete[] = {
1.9f,-0.3f, -0.2f,