-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwmLibjni.cpp
More file actions
173 lines (129 loc) · 4.04 KB
/
wmLibjni.cpp
File metadata and controls
173 lines (129 loc) · 4.04 KB
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
#include <jni.h>
#include <stdio.h>
#include <string.h>
#include <opencv2/opencv.hpp>
#include <vector>
#include "WatermarkAPI.h"
#include "watermark_wmLib.h"
#define WATERMARK_WIDTH 256
#define WATERMARK_HEIGHT 256
extern "C" {
//read the region of the img and change ycrcb, then return y channel
cv::Mat GetImageData(cv::Mat& img, cv::Rect region)
{
cv::Mat data = img(region);
cv::Mat imgycc;
cv::cvtColor(data, imgycc, cv::COLOR_BGR2YCrCb);
std::vector<cv::Mat> imgchannels;
cv::split(imgycc, imgchannels);
cv::Mat result = imgchannels[0].clone();
return result;
}
//put ychannel image back to image
void PutImageData(cv::Mat& img, cv::Rect region, cv::Mat& ychannel)
{
cv::Mat data = img(region);
cv::Mat imgycc;
cv::cvtColor(data, imgycc, cv::COLOR_BGR2YCrCb);
std::vector<cv::Mat> imgchannels;
cv::split(imgycc, imgchannels);
ychannel.copyTo(imgchannels[0]);
cv::merge(imgchannels, imgycc);
cv::cvtColor(imgycc, data, cv::COLOR_YCrCb2BGR);
data.copyTo(img(region));
}
//imgfile : image filename which watermark will embed to
//wminfo : watermark info, eight numbers, each number '1'-'6'
//x,y : position of the watermark
//strength: strength of watermark
JNIEXPORT jint JNICALL Java_watermark_wmLib_addwm(JNIEnv * env,
jobject obj,
jbyteArray imgfile,
jbyteArray wminfo,
jint x,
jint y,
jint strength)
{
jbyte * filebuf;
jbyte * wmbuf;
filebuf = env->GetByteArrayElements(imgfile,JNI_FALSE);
wmbuf = env->GetByteArrayElements(wminfo,JNI_FALSE);
if (filebuf == NULL || wmbuf == NULL)
{
fprintf(stderr,"image file name or watermark must not empty\n");
return -1;
}
//read image file
cv::Mat img = cv::imread((char*)filebuf);
if (img.empty())
{
fprintf(stderr,"image file %s can't be opened, please check!\n",filebuf);
return -2;
}
if (img.rows < WATERMARK_HEIGHT || img.cols < WATERMARK_WIDTH ||
img.channels() != 3 )
{
fprintf(stderr,"image file size or channels not correct, must >= 256X256, RGB\n");
return -3;
}
//maybe should check the region
cv::Rect rg(x,y,WATERMARK_WIDTH,WATERMARK_HEIGHT);
cv::Mat wmImg = GetImageData(img, rg);
AddWatermark256((unsigned char*)wmImg.data,
(char*)wmbuf,
(int)strength);
PutImageData(img, rg, wmImg);
//save image file
std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 100};
cv::imwrite((char*)filebuf,img, params);
char newfile[256];
strcpy(newfile,(char*)filebuf);
strcat(newfile,".wm.jpg");
cv::imwrite(newfile, wmImg, params);
fprintf(stderr,"writing temp file %s\n",newfile);
env->ReleaseByteArrayElements(imgfile, filebuf, 0);
env->ReleaseByteArrayElements(wminfo, wmbuf, 0);
return 0;
}
JNIEXPORT jint JNICALL Java_watermark_wmLib_detwm(JNIEnv * env,
jobject obj,
jbyteArray imgfile,
jbyteArray wminfo,
jint x,
jint y)
{
jbyte * filebuf;
jbyte * wmbuf;
filebuf = env->GetByteArrayElements(imgfile,JNI_FALSE);
wmbuf = env->GetByteArrayElements(wminfo,JNI_FALSE);
if (filebuf == NULL || wmbuf == NULL)
{
fprintf(stderr,"image file name or watermark must not empty\n");
return -1;
}
//read image file
cv::Mat img = cv::imread((char*)filebuf);
if (img.empty())
{
fprintf(stderr,"image file %s can't be opened, please check!\n",filebuf);
return -2;
}
if (img.rows < WATERMARK_HEIGHT || img.cols < WATERMARK_WIDTH ||
img.channels() != 3 )
{
fprintf(stderr,"image file size or channels not correct, must >= 256X256, RGB\n");
return -3;
}
//maybe should check the region
cv::Rect rg(x,y,WATERMARK_WIDTH,WATERMARK_HEIGHT);
cv::Mat wmImg = GetImageData(img, rg);
char wm[9] = {0};
DetectWatermark256((unsigned char*)wmImg.data,
wm);
fprintf(stderr, "DetectWatermark256 return : %s\n", wm);
memcpy(wmbuf, wm, 9);
env->ReleaseByteArrayElements(imgfile, filebuf, 0);
env->ReleaseByteArrayElements(wminfo, wmbuf, 0);
return 0;
}
}