|
@ -17,12 +17,17 @@ import android.database.Cursor;
|
17
|
17
|
import android.graphics.Bitmap;
|
18
|
18
|
import android.graphics.BitmapFactory;
|
19
|
19
|
import android.graphics.Canvas;
|
|
20
|
import android.graphics.Color;
|
|
21
|
import android.graphics.Paint;
|
|
22
|
import android.graphics.Path;
|
20
|
23
|
import android.graphics.Rect;
|
21
|
24
|
import android.net.Uri;
|
22
|
25
|
import android.os.Bundle;
|
23
|
26
|
import android.provider.MediaStore;
|
|
27
|
import android.util.Log;
|
24
|
28
|
|
25
|
29
|
import com.ai.ipu.basic.file.FileUtil;
|
|
30
|
import com.ai.ipu.basic.string.StringUtil;
|
26
|
31
|
import com.ai.ipu.mobile.app.IpuAppInfo;
|
27
|
32
|
import com.ai.ipu.mobile.frame.IIpuMobile;
|
28
|
33
|
import com.ai.ipu.mobile.frame.plugin.Plugin;
|
|
@ -40,6 +45,8 @@ import com.ailk.common.data.impl.DataMap;
|
40
|
45
|
* @desc 图片和照相机功能集合
|
41
|
46
|
*/
|
42
|
47
|
public class MobileCamera extends Plugin {
|
|
48
|
private static final int DEFAULT_COLOR = 0xffdcdcdc;
|
|
49
|
|
43
|
50
|
public enum TYPE {
|
44
|
51
|
BASE64_AND_PATH(2), //Base64编码和文件路径
|
45
|
52
|
PATH(1), //文件路径
|
|
@ -295,80 +302,195 @@ public class MobileCamera extends Plugin {
|
295
|
302
|
//cursor.close(); //关闭会报错
|
296
|
303
|
return path;
|
297
|
304
|
}
|
298
|
|
|
299
|
|
public void addWaterMarkForImage(JSONArray param) throws Exception {
|
300
|
|
String path = param.getString(0);
|
301
|
|
String waterPath = param.getString(1);
|
302
|
|
double left = param.getDouble(2);
|
303
|
|
double top = param.getDouble(3);
|
304
|
|
double right = param.getDouble(4);
|
305
|
|
double bottom = param.getDouble(5);
|
306
|
|
|
307
|
|
String resultPath = addWaterMark(path, waterPath, left, top, right, bottom);
|
308
|
|
|
|
305
|
|
|
306
|
|
|
307
|
public void addImageWaterMarkForImage(JSONArray param) throws Exception {
|
|
308
|
String picPath = param.getString(0);
|
|
309
|
String mark = param.getString(1);
|
|
310
|
String p = param.getString(2);
|
|
311
|
if (p== null || !StringUtil.isDataMap(p)) {
|
|
312
|
throw new IllegalArgumentException("没有DataMap参数");
|
|
313
|
}
|
|
314
|
DataMap dataMap = new DataMap(p);
|
|
315
|
double x = dataMap.getDouble("markX");
|
|
316
|
double y = dataMap.getDouble("markY");
|
|
317
|
double width = dataMap.getDouble("width", 0.2);
|
|
318
|
String resultPath = addWaterMark(picPath, mark, x, y, width);
|
|
319
|
|
309
|
320
|
callback(resultPath);
|
310
|
321
|
}
|
311
|
|
|
|
322
|
|
|
323
|
public void addTextWaterMarkForImage(JSONArray params) throws Exception {
|
|
324
|
String res = null;
|
|
325
|
String picPath = params.getString(0);
|
|
326
|
String text = params.getString(1);
|
|
327
|
String p = params.getString(2);
|
|
328
|
if (p== null || !StringUtil.isDataMap(p)) {
|
|
329
|
throw new IllegalArgumentException("没有DataMap参数");
|
|
330
|
}
|
|
331
|
DataMap dataMap = new DataMap(p);
|
|
332
|
double textSize = dataMap.getDouble("textSize", 0.05);
|
|
333
|
String textColor = dataMap.getString("textColor");
|
|
334
|
double angle = dataMap.getDouble("angle");
|
|
335
|
double rowSpaceRatio, colSpaceRatio, markXRatio, markYRatio;
|
|
336
|
boolean isRepeat = dataMap.getBoolean("isRepeat");
|
|
337
|
if (isRepeat) {
|
|
338
|
rowSpaceRatio = dataMap.getDouble("rowSpace", 0.2);
|
|
339
|
colSpaceRatio = dataMap.getDouble("colSpace", 0.2);
|
|
340
|
res = addText(picPath, text, textSize, textColor, angle, rowSpaceRatio, colSpaceRatio);
|
|
341
|
} else {
|
|
342
|
markXRatio = dataMap.getDouble("markX");
|
|
343
|
markYRatio = dataMap.getDouble("markY");
|
|
344
|
res = addText(picPath, text, markXRatio, markYRatio, textSize, textColor, angle);
|
|
345
|
}
|
|
346
|
|
|
347
|
callback(res);
|
|
348
|
}
|
|
349
|
|
|
350
|
/**
|
|
351
|
* 在指定位置添加一行文字水印
|
|
352
|
* @param picPath
|
|
353
|
* @param text
|
|
354
|
* @param markXRatio
|
|
355
|
* @param markYRatio
|
|
356
|
* @param textSizeRatio
|
|
357
|
* @param textColor
|
|
358
|
* @param angle
|
|
359
|
* @return
|
|
360
|
*/
|
|
361
|
private String addText(String picPath, String text, double markXRatio, double markYRatio, double textSizeRatio, String textColor, double angle) {
|
|
362
|
String result;
|
|
363
|
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
|
|
364
|
bitmap = bitmap.copy(bitmap.getConfig(), true);
|
|
365
|
int picWidth = bitmap.getWidth();
|
|
366
|
Canvas canvas = new Canvas(bitmap);
|
|
367
|
Paint paint = new Paint();
|
|
368
|
paint.setColor(textColor == null ? 0xffdcdcdc : Color.parseColor(textColor)); //默认灰色
|
|
369
|
int textSize = (int) (picWidth * textSizeRatio);
|
|
370
|
int x = (int) (markXRatio * picWidth);
|
|
371
|
int y = (int) (markYRatio * bitmap.getHeight());
|
|
372
|
paint.setTextSize(textSize); //图片宽度的20分之一作为字体大小
|
|
373
|
|
|
374
|
// 用path确定文字位置和方向
|
|
375
|
Path path = new Path();
|
|
376
|
double radians = Math.toRadians(angle);
|
|
377
|
x -= textSize * Math.sin(radians);
|
|
378
|
y += textSize * Math.cos(radians);
|
|
379
|
path.moveTo(x, y);
|
|
380
|
int textLength = (int) paint.measureText(text);
|
|
381
|
x += textLength;
|
|
382
|
y += textLength * Math.tan(radians);
|
|
383
|
path.lineTo(x, y);
|
|
384
|
|
|
385
|
canvas.drawTextOnPath(text, path, 0, 0, paint);
|
|
386
|
result = saveBmpFile(bitmap);
|
|
387
|
return result;
|
|
388
|
}
|
|
389
|
|
|
390
|
/**
|
|
391
|
* 平铺模式添加文字水印
|
|
392
|
* @param picPath
|
|
393
|
* @param text
|
|
394
|
* @param textSizeRatio
|
|
395
|
* @param textColor
|
|
396
|
* @param angle
|
|
397
|
* @param rowSpaceRatio
|
|
398
|
* @param colSpaceRatio
|
|
399
|
* @return
|
|
400
|
*/
|
|
401
|
private String addText(String picPath, String text, double textSizeRatio, String textColor, double angle, double rowSpaceRatio, double colSpaceRatio) {
|
|
402
|
String result;
|
|
403
|
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
|
|
404
|
bitmap = bitmap.copy(bitmap.getConfig(), true);
|
|
405
|
int picWidth = bitmap.getWidth();
|
|
406
|
int picHeight = bitmap.getHeight();
|
|
407
|
Canvas canvas = new Canvas(bitmap);
|
|
408
|
Paint paint = new Paint();
|
|
409
|
paint.setColor(textColor == null ? DEFAULT_COLOR : Color.parseColor(textColor)); //默认灰色
|
|
410
|
int textSize = (int) (textSizeRatio * picWidth);
|
|
411
|
paint.setTextSize(textSize);
|
|
412
|
canvas.translate(picWidth / 2, picHeight / 2);
|
|
413
|
canvas.rotate((float) angle);
|
|
414
|
|
|
415
|
int textWidth = (int) paint.measureText(text);
|
|
416
|
int l = Math.max(picHeight, picWidth) * 3 / 2;
|
|
417
|
int colSpace = (int) (rowSpaceRatio * picWidth);
|
|
418
|
int rowSpace = (int) (colSpaceRatio * picHeight);
|
|
419
|
for (int x = -l/2; x<l/2; x+=colSpace+textWidth) {
|
|
420
|
for(int y=-l/2;y<l/2;y+=rowSpace) {
|
|
421
|
canvas.drawText(text, x, y, paint);
|
|
422
|
}
|
|
423
|
}
|
|
424
|
result = saveBmpFile(bitmap);
|
|
425
|
return result;
|
|
426
|
}
|
|
427
|
|
312
|
428
|
/**
|
313
|
429
|
* @param path 原图片路径
|
314
|
430
|
* @param waterPath 水印图片路径
|
315
|
|
* @param left 水印图片左边位置,值为占整个图片的宽的比例
|
316
|
|
* @param top 水印图片上边位置,值为占整个图片的高的比例
|
317
|
|
* @param right 水印图片右边位置,值为占整个图片的宽的比例
|
318
|
|
* @param bottom 水印图片下边位置,值为占整个图片的高的比例
|
|
431
|
* @param x 水印图片左边位置,值为占整个图片的宽的比例
|
|
432
|
* @param y 水印图片上边位置,值为占整个图片的高的比例
|
|
433
|
* @param width 水印图片宽度,值为占整个图片的宽的比例
|
319
|
434
|
* @return 加了水印的图片路径
|
320
|
435
|
*/
|
321
|
|
private String addWaterMark(String path, String waterPath, double left, double top, double right, double bottom) {
|
|
436
|
private String addWaterMark(String path, String waterPath, double x, double y, double width) throws Exception {
|
322
|
437
|
Bitmap bitmap1 = BitmapFactory.decodeFile(path);
|
323
|
438
|
Bitmap bitmap2 = BitmapFactory.decodeFile(waterPath);
|
324
|
439
|
bitmap1 = bitmap1.copy(bitmap1.getConfig(), true);
|
325
|
440
|
Canvas canvas = new Canvas(bitmap1);
|
326
|
441
|
|
327
|
|
Rect rect = buildRect(bitmap1, left, top, right, bottom);
|
|
442
|
Rect rect = buildRect(bitmap1, bitmap2, x, y, width);
|
328
|
443
|
canvas.drawBitmap(bitmap2, null, rect, null);
|
329
|
444
|
|
330
|
|
String fileName = format.format(new Date()) + "-mark.png";
|
331
|
|
File f = new File(DirectionUtil.getInstance(context).getImageDirection(true), fileName);
|
332
|
|
if (!f.getParentFile().exists()) {
|
333
|
|
f.getParentFile().mkdirs();
|
334
|
|
}
|
335
|
|
String res = f.getAbsolutePath();
|
336
|
|
BufferedOutputStream bos = null;
|
337
|
|
try {
|
338
|
|
FileOutputStream outputStream = new FileOutputStream(f);
|
339
|
|
bos = new BufferedOutputStream(outputStream);
|
340
|
|
bitmap1.compress(Bitmap.CompressFormat.PNG, 100, bos);
|
341
|
|
} catch (FileNotFoundException e) {
|
342
|
|
e.printStackTrace();
|
343
|
|
res = null;
|
344
|
|
} finally {
|
345
|
|
if (bos != null) {
|
346
|
|
try {
|
347
|
|
bos.close();
|
348
|
|
} catch (IOException e) {
|
349
|
|
e.printStackTrace();
|
350
|
|
}
|
351
|
|
}
|
352
|
|
}
|
|
445
|
String res = saveBmpFile(bitmap1);
|
353
|
446
|
|
354
|
447
|
return res;
|
355
|
448
|
}
|
356
|
449
|
|
357
|
|
private Rect buildRect(Bitmap bitmap, double left, double top, double right, double bottom) {
|
358
|
|
left = fixIn1(left);
|
359
|
|
right = fixIn1(right);
|
360
|
|
top = fixIn1(top);
|
361
|
|
bottom = fixIn1(bottom);
|
362
|
|
int w = bitmap.getWidth();
|
363
|
|
int h = bitmap.getHeight();
|
364
|
|
int l = (int) (w * left);
|
365
|
|
int t = (int) (h * top);
|
366
|
|
int r = (int) (w * right);
|
367
|
|
int b = (int) (h * bottom);
|
368
|
|
if (l >= r || t >= b) {
|
369
|
|
IpuMobileLog.w(TAG, "buildRect: invalid params");
|
370
|
|
return new Rect(0, 0, w/4, h/4);
|
371
|
|
}
|
|
450
|
/**
|
|
451
|
* 将bitmap保存到ipu应用的image目录,命名为“日期-mark.png”
|
|
452
|
* @param bitmap
|
|
453
|
* @return 文件路径,失败返回null
|
|
454
|
*/
|
|
455
|
String saveBmpFile(Bitmap bitmap) {
|
|
456
|
String fileName = format.format(new Date()) + "-mark.png";
|
|
457
|
File f = new File(DirectionUtil.getInstance(context).getImageDirection(true), fileName);
|
|
458
|
if (!f.getParentFile().exists()) {
|
|
459
|
f.getParentFile().mkdirs();
|
|
460
|
}
|
|
461
|
String res = f.getAbsolutePath();
|
|
462
|
BufferedOutputStream bos = null;
|
|
463
|
try {
|
|
464
|
FileOutputStream outputStream = new FileOutputStream(f);
|
|
465
|
bos = new BufferedOutputStream(outputStream);
|
|
466
|
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
|
|
467
|
} catch (FileNotFoundException e) {
|
|
468
|
Log.e(TAG, "addWaterMark: ", e);
|
|
469
|
res = null;
|
|
470
|
} finally {
|
|
471
|
if (bos != null) {
|
|
472
|
try {
|
|
473
|
bos.close();
|
|
474
|
} catch (IOException e) {
|
|
475
|
Log.e(TAG, "addWaterMark: ", e);
|
|
476
|
}
|
|
477
|
}
|
|
478
|
}
|
|
479
|
|
|
480
|
return res;
|
|
481
|
}
|
|
482
|
|
|
483
|
private Rect buildRect(Bitmap pic, Bitmap mark, double xPercent, double yPercent, double wPercent) throws Exception {
|
|
484
|
xPercent = fixIn1(xPercent);
|
|
485
|
yPercent = fixIn1(yPercent);
|
|
486
|
wPercent = fixIn1(wPercent);
|
|
487
|
int w = pic.getWidth();
|
|
488
|
int h = pic.getHeight();
|
|
489
|
int l = (int) (w * xPercent);
|
|
490
|
int t = (int) (h * yPercent);
|
|
491
|
int r = (int) (w * wPercent + l);
|
|
492
|
int b = (int) (w * wPercent * mark.getHeight() / mark.getWidth() + t);
|
|
493
|
|
372
|
494
|
Rect rect = new Rect(l, t, r, b);
|
373
|
495
|
return rect;
|
374
|
496
|
}
|