前沿

不同的app可能需要加上不同的logo标识这张图片归属问题,比如微博图片可能涉及到原图版权,或者涉及到个人信息认证敏感信息,从安全的角度来说也需要加上特定的文本logo用于标识此图的所属用途。

需求:

拍照后加入文本水印合成一张图。

实现:

/**

* 给一张Bitmap添加水印文字。

*

* @param bitmap 源图片

* @param content 水印文本

* @param textSize 水印字体大小 ,单位pix。

* @param color 水印字体颜色。

* @param x 起始坐标x

* @param y 起始坐标y

*@param positionFlag 居左/居右

* @param recycle 是否回收

* @return 已经添加水印后的Bitmap。

*/

public static Bitmap addTextWatermark(Bitmap bitmap, String content, int textSize, int color, float x, float y,boolean positionFlag, boolean recycle) {

if (isEmptyBitmap(bitmap) || content == null)

return null;

Bitmap ret = bitmap.copy(bitmap.getConfig(), true);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

Canvas canvas = new Canvas(ret);

paint.setColor(color);

paint.setTextSize(textSize);

Rect bounds = new Rect();

paint.getTextBounds(content, 0, content.length(), bounds);

canvas.drawText(content, bitmap.getWidth() - x - bounds.width() - bounds.left, bitmap.getHeight() - bounds.height() - bounds.top - y, paint);

if (positionFlag) {

canvas.drawText(content, x, bitmap.getHeight() - bounds.height() - bounds.top - y, paint);

} else {

canvas.drawText(content, bitmap.getWidth() - x - bounds.width() - bounds.left, bitmap.getHeight() - bounds.height() - bounds.top - y, paint);

}

if (recycle && !bitmap.isRecycled())

bitmap.recycle();

return ret;

}

/**

* Bitmap对象是否为空。

*/

public static boolean isEmptyBitmap(Bitmap src) {

return src == null || src.getWidth() == 0 || src.getHeight() == 0;

}

@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

Log.i(TAG, requestCode + " " + resultCode + " ");

// 拍照返回处理

if (requestCode == CAMERA_WITH_DATA && resultCode == Activity.RESULT_OK) {

try {

int degree = readPictureDegree(photoFilePath);

//压缩覆盖一下

Bitmap bm = thumbnailLoader.resizeBitmap(photoFilePath,

UIUtils.getScreenWidth(activityContext),

UIUtils.getScreenHeight(activityContext));

Bitmap degreeBM = null;

try {

//如果遇到图片旋转,纠正一下

if (degree != 0) {

//旋转图片 动作

Matrix matrix = new Matrix();

matrix.postRotate(degree);

// 创建新的图片

degreeBM = Bitmap.createBitmap(bm, 0, 0,

bm.getWidth(), bm.getHeight(), matrix, true);

}

} catch (Exception e) {

e.printStackTrace();

}

if (degreeBM != null) {

bm = degreeBM;

}

Bitmap bp= BitmapUtils.addTextWatermark(bm,"水印"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()),30, Color.WHITE,50.0f,50.0f,false,true);

if (bp != null) {

bm = bp;

}

FileOutputStream out = new FileOutputStream(photoFilePath);

bm.compress(Bitmap.CompressFormat.JPEG, 100, out);

out.flush();

out.close();

if (degreeBM != null && !degreeBM.isRecycled()) {

degreeBM.recycle();

}

if (bp != null && !bp.isRecycled()) {

bp.recycle();

}

if (bm != null && !bm.isRecycled()) {

bm.recycle();

}

IssueImageEntity li = new IssueImageEntity();

li.setPath(photoFilePath);

li.setUploadStatus("N");

photoList.add(li);

photoAdapter.notifyDataSetChanged();

} catch (Exception e) {

e.printStackTrace();

Toast.makeText(activityContext, "可能因硬件原因压缩照片异常 请重新拍照!" + "(" + e.getMessage() + ")", Toast.LENGTH_SHORT).show();

logManager.wirteLog("拍照", "拍照异常" + LogThrowableManager.getThrowableInfo(e));

}

}

还有就是加入图片中加入图片水印

/**

* 给图片添加水印,水印会根据图片宽高自动缩放处理

*

* @param watermark 水印

* @param image 添加水印的图片

* @param offsetX 添加水印的X轴偏移量

* @param offsetY 添加水印的Y轴偏移量

* @param srcWaterMarkImageWidth 水印对应的原图片宽度,即ui制作水印时候参考的图片画布宽度,应该是已知的图片最大宽度

* @param addInLeft true 在左下角添加水印,false 在右下角添加水印

*/

public static void addWatermark(Bitmap watermark, Bitmap image, int srcWaterMarkImageWidth, int offsetX, int offsetY, boolean addInLeft) {

int imageWidth = image.getWidth();

int imageHeight = image.getHeight();

if (0 == imageWidth || 0 == imageHeight) {

throw new RuntimeException("AlbumBuilder: 加水印的原图宽或高不能为0!");

}

int watermarkWidth = watermark.getWidth();

int watermarkHeight = watermark.getHeight();

float scale = imageWidth / (float) srcWaterMarkImageWidth;

if (scale > 1) scale = 1;

else if (scale < 0.4) scale = 0.4f;

int scaleWatermarkWidth = (int) (watermarkWidth * scale);

int scaleWatermarkHeight = (int) (watermarkHeight * scale);

Bitmap scaleWatermark = Bitmap.createScaledBitmap(watermark, scaleWatermarkWidth, scaleWatermarkHeight, true);

Canvas canvas = new Canvas(image);

Paint paint = new Paint();

paint.setAntiAlias(true);

if (addInLeft) {

canvas.drawBitmap(scaleWatermark, offsetX, imageHeight - scaleWatermarkHeight - offsetY, paint);

} else {

canvas.drawBitmap(scaleWatermark, imageWidth - offsetX - scaleWatermarkWidth, imageHeight - scaleWatermarkHeight - offsetY, paint);

}

recycle(scaleWatermark);

}

/**

* 给图片添加带文字和图片的水印,水印会根据图片宽高自动缩放处理

*

* @param watermark 水印图片

* @param image 要加水印的图片

* @param srcWaterMarkImageWidth 水印对应的原图片宽度,即ui制作水印时候参考的图片画布宽度,应该是已知的图片最大宽度

* @param text 要添加的文字

* @param offsetX 添加水印的X轴偏移量

* @param offsetY 添加水印的Y轴偏移量

* @param addInLeft true 在左下角添加水印,false 在右下角添加水印

*/

public static void addWatermarkWithText(@NonNull Bitmap watermark, Bitmap image, int srcWaterMarkImageWidth, @NonNull String text, int offsetX, int offsetY, boolean addInLeft) {

float imageWidth = image.getWidth();

float imageHeight = image.getHeight();

if (0 == imageWidth || 0 == imageHeight) {

throw new RuntimeException("AlbumBuilder: 加水印的原图宽或高不能为0!");

}

float watermarkWidth = watermark.getWidth();

float watermarkHeight = watermark.getHeight();

float scale = imageWidth / (float) srcWaterMarkImageWidth;

if (scale > 1) scale = 1;

else if (scale < 0.4) scale = 0.4f;

float scaleWatermarkWidth = watermarkWidth * scale;

float scaleWatermarkHeight = watermarkHeight * scale;

Bitmap scaleWatermark = Bitmap.createScaledBitmap(watermark, (int) scaleWatermarkWidth, (int) scaleWatermarkHeight, true);

Canvas canvas = new Canvas(image);

Paint textPaint = new TextPaint();

textPaint.setAntiAlias(true);

textPaint.setColor(Color.WHITE);

float textsize = (float) (scaleWatermark.getHeight() * 2) / (float) 3;

textPaint.setTextSize(textsize);

Rect textRect = new Rect();

textPaint.getTextBounds(text, 0, text.length(), textRect);

if (addInLeft) {

canvas.drawText(text, scaleWatermarkWidth + offsetX, imageHeight - textRect.height() - textRect.top - offsetY, textPaint);

} else {

canvas.drawText(text, imageWidth - offsetX - textRect.width() - textRect.left, imageHeight - textRect.height() - textRect.top - offsetY, textPaint);

}

Paint sacleWatermarkPaint = new Paint();

sacleWatermarkPaint.setAntiAlias(true);

if (addInLeft) {

canvas.drawBitmap(scaleWatermark, offsetX, imageHeight - textRect.height() - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);

} else {

canvas.drawBitmap(scaleWatermark, imageWidth - textRect.width() - offsetX - scaleWatermarkWidth / 6, imageHeight - textRect.height() - offsetY - scaleWatermarkHeight / 6, sacleWatermarkPaint);

}

recycle(scaleWatermark);

}

/**

* 保存Bitmap到指定文件夹

*

* @param act 上下文

* @param dirPath 文件夹全路径

* @param bitmap bitmap

* @param namePrefix 保存文件的前缀名,文件最终名称格式为:前缀名+自动生成的唯一数字字符+.png

* @param notifyMedia 是否更新到媒体库

* @param callBack 保存图片后的回调,回调已经处于UI线程

*/

public static void saveBitmapToDir(final Activity act, final String dirPath, final String namePrefix, final Bitmap bitmap, final boolean notifyMedia, final SaveBitmapCallBack callBack) {

new Thread(new Runnable() {

@Override

public void run() {

File dirF = new File(dirPath);

if (!dirF.exists() || !dirF.isDirectory()) {

if (!dirF.mkdirs()) {

act.runOnUiThread(new Runnable() {

@Override

public void run() {

callBack.onCreateDirFailed();

}

});

return;

}

}

try {

final File writeFile = File.createTempFile(namePrefix, ".png", dirF);

FileOutputStream fos = null;

fos = new FileOutputStream(writeFile);

bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

fos.flush();

fos.close();

if (notifyMedia) {

// EasyPhotos.notifyMedia(act, writeFile);

}

act.runOnUiThread(new Runnable() {

@Override

public void run() {

callBack.onSuccess(writeFile);

}

});

} catch (final IOException e) {

act.runOnUiThread(new Runnable() {

@Override

public void run() {

callBack.onIOFailed(e);

}

});

}

}

}).start();

}

android中用点标识路径,Android Bitmap、路径、图片加上文本水印相关推荐

  1. Android设备唯一标识(终极方案!)

    Android设备唯一标识 背景 Android系统中并没有可靠获取所有厂商设备唯一ID的方法,各个方法都有自己的使用范围和局限性,这也是目前流行的Android系统版本过多,设备也是来自不同厂商,且 ...

  2. android系统profile文件路径,Android Profile Tools 入门

    本次分享会目的 通过对增加对 Android Profile Tools 的了解,提高大家工作过程中定位和解决Bug的效率. 注意事项 为了不耽误大家时间,语速可能会快一些 如果讲到某个工具或者技巧, ...

  3. android获取元素路径,Appium元素定位(name、classname、相对路径、绝对路径\、list)...

    元素通过name元素定位 Appium的name元素定位对应的是Android上的text字段,语法: driver.find_element_by_name('安检测速').click() 缺点:部 ...

  4. android 解决小米手机上选择照片路径为null的问题

    之前做了一个获取相册选择图片的功能,后来测试人员在小米的手机 测试时出现崩溃现象.自己就在网上查找资料,发现是小米的获取图片路径的代码与其他的手机不一样,于是修改了代码,解决了这个问题,这里记录一下. ...

  5. android外置sd大小,android 读取外置和内置存储卡路径和大小

    [实例简介] android 读取外置和内置存储卡路径和大小,亲测好使,项目中以运用 [实例截图] [核心代码] a81fbea6-cb7a-4c96-be21-d52578f6de0a └── SD ...

  6. delphi android路径 TPath 文件路径,文件管理

    获取Android相关文档路径 delphi 新路径.文件功能 IOUtils单元,文件路径,文件管理 http://docwiki.embarcadero.com/RADStudio/Berlin/ ...

  7. Android使用sqlliteOpenhelper更改数据库的存储路径放到SD卡上

    假设使用默认的系统管理,默认放在包以下.比較省心.并且在卸载app后不会造成数据残留.可是这样也有一个问题.比方我做一个背单词的软件,那么当用户卸载掉这个app时,他辛辛苦苦下载的单词库也没了... ...

  8. android studio视频路径,Android studio相关设置及实现存在于工程目录中的视频播放...

    一:相关设置 1:主题设置 File-->Settings-->Appearance &Behavior-->Appearance-->THeme 2:Java源码的颜 ...

  9. android 获取sdcard 禁用sdcard,Android获取内置sdcard跟外置sdcard路径

    Android获取内置sdcard跟外置sdcard路径 Android获取内置sdcard跟外置sdcard路径.(测试过两个手机,亲测可用) 1.先得到外置sdcard路径,这个接口是系统提供的标 ...

最新文章

  1. 进程间通信--命名管道
  2. 用 Docker 构建、运行、发布来一个 Spring Boot 应用
  3. CF Gym102028G Shortest Paths on Random Forests
  4. java中大数开方_Java中的大数运算
  5. android view知识点 总结
  6. 斑马Zebra驱动下载
  7. 【ArcGIS风暴】ArcGIS解决数字化之前创建图层时未定义坐标系而导致数据跑偏的问题
  8. 我怀疑全国最会吹牛的人,都在这8个公众号上了
  9. C++ stringstream输入方式
  10. java通用编码规范考试_《java编码规范考试题答案》.doc
  11. Android中 ExpandableList的使用2
  12. 虚拟机linux快捷键,虚拟机控制与Linux快捷键
  13. 如何精确理解leader布置的任务
  14. OSChina 周一乱弹 ——女人比代码复杂多了,搞不懂!
  15. 哈佛机构与冯诺依曼架构
  16. 荣耀20android版本,荣耀法国:荣耀V20/20系列可正常升级Android Q
  17. java背单词软件_背单词的java小软件
  18. 【文献摘录】FaceRevelio: A Face Liveness Detection System forSmartphones with a Single Front Camera
  19. java jit技术_JVM之JIT
  20. python模拟点击后获取状态码_Python获取网页状态码

热门文章

  1. VsCode使用笔记【1】Ubuntu+vscode+Monokai+修改主题中的字体颜色
  2. python用pandas读取excel_Python使用pandas处理Excel
  3. vscode remote-ssh连接ubuntu子系统提示错误:WSL: VSCode server install fails on Ubuntu 19.10
  4. spring常用注解剖析
  5. PS制作圆角透明图片
  6. 升级版的冒泡排序Java
  7. 国际青少年计算机技能大赛英语,竞赛:2017中学生英语能力、青少年信息学奥赛!...
  8. 【数值分析×机器学习】以SVD的分解形式进行深度神经网络的训练(逐渐熟练)
  9. SQL server身份验证
  10. 推送系统从0到1(八):个性化精准推送的实现