Skip to content

Commit

Permalink
滤镜效果 添加马赛克滤镜
Browse files Browse the repository at this point in the history
  • Loading branch information
siwangqishiq committed Mar 19, 2018
1 parent 9c24fa3 commit 459dfe4
Show file tree
Hide file tree
Showing 18 changed files with 160 additions and 254 deletions.
1 change: 0 additions & 1 deletion demo/demo.iml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/instant-run-support" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/javaPrecompile" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/jniLibs" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/lint" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/manifests" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/prebuild" />
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/reload-dex" />
Expand Down
5 changes: 5 additions & 0 deletions imageeditlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
// externalNativeBuild {
// ndkBuild {
// path 'jni/Android.mk'
// }
// }
}

dependencies {
Expand Down
10 changes: 5 additions & 5 deletions imageeditlibrary/jni/colour_space.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@
* limitations under the License.
*/

#include <colour_space.h>
#include "colour_space.h"
#include <math.h>
#include <android/log.h>

#define LOG_TAG "colour_space.c"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

unsigned char convert(float val) {
return floorf((255 * val) + 0.5f);
}

void rgbToHsb(unsigned char red, unsigned char green, unsigned char blue, HSBColour* hsb) {
float min, max;
if (red < green) {
Expand Down Expand Up @@ -82,10 +86,6 @@ void getBrightness(unsigned char red, unsigned char green, unsigned char blue, f
(*brightness) = max/255;
}

inline unsigned char convert(float val) {
return floorf((255 * val) + 0.5f);
}

void hsbToRgb(HSBColour* hsb, unsigned char* red, unsigned char* green, unsigned char* blue) {
if ((*hsb).s == 0) {
*red = *green = *blue = convert((*hsb).b);
Expand Down
2 changes: 2 additions & 0 deletions imageeditlibrary/jni/colour_space.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ typedef struct {
float s; /* Saturation between 0.0 (gray) and 1.0 */
float b; /* Value between 0.0 (black) and 1.0 */
} HSBColour ;


132 changes: 128 additions & 4 deletions imageeditlibrary/jni/photo_processing.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
#include <bitmap.h>
#include <mem_utils.h>
#include <android/log.h>
#include <android/bitmap.h>
#include "beauty.h"

#define LOG_TAG "IMAGE_EDIT_PROCESSING"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

#define COLOR_ARGB(a, r, g, b) ((a)<<24)|((b) << 16)|((g)<< 8)|(r)

void *do_mosaic(void *pix, void *out_pix, unsigned int width, unsigned int height, unsigned int stride,
unsigned int out_stride, unsigned int radius);

static Bitmap bitmap;
//Java_com_xinlan_imageeditlibrary_editimage_fliter_PhotoProcessing
int Java_com_xinlan_imageeditlibrary_editimage_fliter_PhotoProcessing_nativeInitBitmap(JNIEnv* env, jobject thiz, jint width, jint height) {
return initBitmapMemory(&bitmap, width, height);
}
Expand Down Expand Up @@ -284,6 +289,61 @@ Java_com_xinlan_imageeditlibrary_editimage_fliter_PhotoProcessing_handleWhiteSki
freeMatrix();
}

JNIEXPORT void JNICALL
Java_com_xinlan_imageeditlibrary_editimage_fliter_PhotoProcessing_nativeMosaic(JNIEnv *env, jclass type, jobject bitmap,
jobject out_bitmap,
jint radius) {
AndroidBitmapInfo info;
void *pixels;
int ret;

AndroidBitmapInfo out_info;
void *out_pixels;

if ((ret = AndroidBitmap_getInfo(env, bitmap, &info)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}


if ((ret = AndroidBitmap_getInfo(env, out_bitmap, &out_info)) < 0) {
LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret);
return;
}

LOGE("Out Bitmap format is %d ", out_info.format);
if (out_info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("out Bitmap info format is not RGBA_8888 !");
return;
}

LOGE("Bitmap format is %d ", info.format);
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
LOGE("Bitmap format is not RGBA_8888 !");
return;
}

if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return;
}

if ((ret = AndroidBitmap_lockPixels(env, bitmap, &pixels)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return;
}

if ((ret = AndroidBitmap_lockPixels(env, out_bitmap, &out_pixels)) < 0) {
LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret);
return;
}

do_mosaic(pixels, out_pixels, info.width, info.height, info.stride, out_info.stride, radius);
LOGE("image size width = %d , height = %d", info.width, info.height);
AndroidBitmap_unlockPixels(env, bitmap);
AndroidBitmap_unlockPixels(env, out_bitmap);
}

void setWhiteSkin(uint32_t *pix, float whiteVal, int width, int height) {
if (whiteVal >= 1.0 && whiteVal <= 10.0) { //1.0~10.0
float a = log(whiteVal);
Expand Down Expand Up @@ -352,7 +412,7 @@ void setSmooth(uint32_t *pix, float smoothValue, int width, int height) {//磨
}

void freeMatrix() {
if (mIntegralMatrix != NULL) {
if (mIntegralMatrix != NULL) {
free(mIntegralMatrix);
mIntegralMatrix = NULL;
}
Expand Down Expand Up @@ -386,7 +446,7 @@ void initBeautiMatrix(uint32_t *pix, int width, int height) {

if (mImageData_yuv == NULL)
mImageData_yuv = (uint8_t *)malloc(sizeof(uint8_t) * width * height * 3);

RGBToYCbCr((uint8_t *) mImageData_rgb, mImageData_yuv, width * height);

initSkinMatrix(pix, width, height);
Expand Down Expand Up @@ -469,7 +529,7 @@ void initIntegralMatrix(int width, int height) {
mIntegralMatrixSqr[offset + j] = mIntegralMatrixSqr[offset + j - 1] + columnSumSqr[j];
}
}

free(columnSum);
free(columnSumSqr);
//delete[] columnSum;
Expand Down Expand Up @@ -533,5 +593,69 @@ void convertIntToArgb(uint32_t pixel, ARGB* argb) {
argb->alpha = (pixel >> 24);
}


//------------------------ beauty module end --------------------------------------


void *do_mosaic(void *pix, void *out_pix, unsigned int width, unsigned int height, unsigned int stride,
unsigned int out_stride, unsigned int radius) {
if (width == 0 || height == 0 || radius <= 1)
return pix;

uint32_t x, y;
uint32_t r_total = 0;
uint32_t g_total = 0;
uint32_t b_total = 0;

uint32_t limit_x = radius;
uint32_t limit_y = radius;

uint32_t i = 0;
uint32_t j = 0;

int32_t *src_pix = (int32_t *) pix;
int32_t *out = (int32_t *) out_pix;

for (y = 0; y < height; y += radius) {
for (x = 0; x < width; x += radius) {
//rgba *line = (rgba *) pix;
limit_y = y + radius > height ? height : y + radius;
limit_x = x + radius > width ? width : x + radius;

// get average rgb
r_total = 0;
g_total = 0;
b_total = 0;
uint32_t count = 0;
for (j = y; j < limit_y; j++) {
for (i = x; i < limit_x; i++) {
int32_t color = src_pix[j * width + i];

uint8_t r = color & 0x000000FF;
uint8_t g = ((color & 0x0000FF00) >> 8);
uint8_t b = ((color & 0x00FF0000) >> 16);

r_total += r;
g_total += g;
b_total += b;

count++;
}//end for i
}//end for j

uint32_t r = r_total / count;
uint32_t g = g_total / count;
uint32_t b = b_total / count;

//ALOGE("total = %d count = %d ", total , count);
for (j = y; j < limit_y; j++) {
for (i = x; i < limit_x; i++) {
out[j * width + i] = COLOR_ARGB(255,r,g,b);
}//end for i
}//end for j
}//end for x
}//end for y

return pix;
}

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public class PhotoProcessing {


public static Bitmap filterPhoto(Bitmap bitmap, int position) {
if(position == 12){//马赛克滤镜
return handleMosaicFliter(bitmap);
}

if (bitmap != null) {
sendBitmapToNative(bitmap);
}

switch (position) {
case 0: // Original
break;
Expand Down Expand Up @@ -116,6 +121,17 @@ public static native void nativeLoadResizedJpegBitmap(byte[] jpegData,

public static native void freeBeautifyMatrix();

public static native void nativeMosaic(Bitmap src,Bitmap out , int radius);

private static Bitmap handleMosaicFliter(Bitmap src){
Bitmap out = null;
if(src!=null){
out = Bitmap.createBitmap(src.getWidth() ,src.getHeight() , Config.ARGB_8888);
nativeMosaic(src,out,32);
}
return out;
}

private static void sendBitmapToNative(Bitmap bitmap) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected Bitmap doInBackground(Integer... params) {
}

srcBitmap = Bitmap.createBitmap(activity.getMainBit().copy(
Bitmap.Config.RGB_565, true));
Bitmap.Config.ARGB_8888, true));
return PhotoProcessing.filterPhoto(srcBitmap, type);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,10 @@ public static Bitmap resizeBitmap( final Bitmap input, int destWidth, int destHe
public static Bitmap getSampledBitmap(String filePath, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(filePath, options);
int inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inSampleSize = inSampleSize;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
Expand Down
Loading

0 comments on commit 459dfe4

Please sign in to comment.