Android-----实现给图片添加字体
实现给图片添加字体,图片旋转功能:xml布局文件内容如下,一个简单的ImageView布局
<com.example.hsjgapp.RotateImageView //这里存放要展示的图片
android:id="@+id/imageViewShow"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:scaleType="matrix" >
</com.example.hsjgapp.RotateImageView>
<ImageView //这里当作点击按钮使用 , 也可以用Button组件
android:id="@+id/rotateImageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="@android:color/transparent"
android:gravity="center_horizontal"
android:src="@drawable/rotate_photo">
</ImageView>
RotateImageView文件内容如下:
public class RotateImageView extends ImageView { public RotateImageView(Context context) {
super(context);
} public RotateImageView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas){ super.onDraw(canvas); }
}
逻辑处理文件MainActivity.java代码内容如下:
String imgShow = localImagePath + theinformation.getJylsh() + "/" + photoType+ "_img" + ".jpg"; //图片路径
Bitmap bmp = getLoacalBitmap(imgShow);//获取图片Bitmap
ImageView view = (ImageView) findViewById(R.id.imageViewShow); //获得ImageView组件
view.setImageBitmap(bmp); //在ImageView组件上展示图片Bitmap
ImageView rotateImageView = (ImageView) findViewById(R.id.rotateImageView);
rotateImageView.setOnClickListener(new OnClickListener() { //每点击一次照片旋转90°,并重新展示水印
int count = 0;
@Override
public void onClick(View view) {
count++;
bmp = getLoacalBitmap(localTempImgDir);//没水印的图 ImageView Imgview = (ImageView) findViewById(R.id.imageViewShow); //照片旋转
Bitmap rotate = setRotate(bmp,count * 90);
//添加字体
resultBitmap = addTextWatermark(rotate,photoType, jyyXm,theinformation.getClsbdh(),
theinformation.getHphm(),theinformation.getHpzl(),theinformation.getJylsh(),true); Imgview.setImageBitmap(resultBitmap);//展示旋转并添加字体后的照片 }
}); /*
另外保存一份添加字体后的照片到指定目录
*/
String ImagePath = localImagePath + theinformation.getJylsh() + "/" + photoType+ "_img" + ".jpg";
File file = new File(ImagePath);
boolean isSuccess = save(resultBitmap,file,true);
/**
* 加载本地图片 http://bbs.3gstdy.com
*
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
try {
Bitmap bmp = BitmapFactory.decodeFile(url);
return bmp;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
} //照片旋转方法 public static Bitmap setRotate(Bitmap map, int rotate){
Matrix matrix = new Matrix();
// 设置旋转角度
matrix.setRotate(rotate);
// 重新绘制Bitmap
map = Bitmap.createBitmap(map, map.getWidth()/10, 0, map.getWidth()*8/10,map.getHeight(), matrix, true);
return map;
}
/**
* 给一张Bitmap添加水印文字。
* @param src 源图片
* //@param content 水印文本
* @param recycle 是否回收
* @return 已经添加水印后的Bitmap。
*/
public Bitmap addTextWatermark(Bitmap src, String strname, String newjyyxm,String clsbdh,
String hphm,String hpzl,String jylsh, boolean recycle) {
if (isEmptyBitmap(src)) {
return null;
}
String str = TimeTool.getTiem();
String sjimei = "imei:"
+ ((TelephonyManager) ImageShowActivity.this
.getSystemService(TELEPHONY_SERVICE)).getDeviceId();
//将sjimei字符串转大写
StringBuffer sb = new StringBuffer();
if(sjimei!=null){
for(int i=0;i<sjimei.length();i++){
char c = sjimei.charAt(i);
sb.append(Character.toUpperCase(c));
}
}
sjimei=sb.toString();
Bitmap ret = src.copy(src.getConfig(), true);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas canvas = new Canvas(ret);
paint.setColor(Color.RED);
paint.setTextSize(25.0f);
Rect bounds = new Rect();
paint.getTextBounds(strname, 0, strname.length(), bounds); canvas.drawText("照片名称:"+strname+" 时间:"+str, 15, 25, paint);// 绘制上去字,开始未知x,y采用那只笔绘制
canvas.drawText(sjimei+" 检验员:" + newjyyxm, 15, 50, paint);
canvas.drawText("流水号:"+jylsh, 15, 75, paint);
canvas.drawText("车牌:"+hphm+" 种类:"+hpzl, 15, 100, paint);
canvas.drawText("型号:"+clsbdh, 15, 125, paint); if (recycle && !src.isRecycled()) {
src.recycle();
}
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return ret;
}
/**
* Bitmap对象是否为空。
*/
public static boolean isEmptyBitmap(Bitmap src) {
return src == null || src.getWidth() == 0 || src.getHeight() == 0;
}
/**
* 保存图片到文件File。
*
* @param src 源图片
* @param path 要保存到的文件
* @param //format 保存格式(PNG、JPEG、webp)
* @param recycle 是否回收
* @return true 成功 false 失败
*/
public boolean save(Bitmap src, File path, boolean recycle) {
if (isEmptyBitmap(src)) {
return false;
}
OutputStream os;
boolean ret = false;
try {
os = new BufferedOutputStream(new FileOutputStream(path));
ret = src.compress(Bitmap.CompressFormat.JPEG, 100, os);
if (recycle && !src.isRecycled())
src.recycle();
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}
Android-----实现给图片添加字体的更多相关文章
- Android控件上添加图片
项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是 ...
- android图像处理系列之四-- 给图片添加边框(上)
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...
- android图像处理系列之四--给图片添加边框(上)
图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...
- Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等
仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...
- android获得ImageView图片的等级
android获得ImageView图片的等级问题 要实现的功能如下图,点击分享能显示选中与不选中状态,然后发送是根据状态来实现具体分享功能. 在gridview中有5个子项,每个子元素都有两张图片A ...
- ios图片添加文字或者水印
在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...
- linux服务器下添加字体
版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 引言:这两天在开发一个动态生成海报的东西(图片拼接,图片水印),开发在windows下没有问题,图片和文字都能正常的生成出来. ...
- php 图片添加文字水印 以及 图片合成(微信快码传播)
1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...
- 13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)
如果在应用中,如果想要给app 添加模糊滤镜,可能第一想到的是第三方类库,比如 Win2d.lumia Imaging SDK .WriteableBitmapEx,不可否认,这些类库功能强大,效果也 ...
随机推荐
- Pandas | 19 合并/连接
Pandas具有功能全面的高性能内存中连接操作,与SQL等关系数据库非常相似.Pandas提供了一个单独的merge()函数,作为DataFrame对象之间所有标准数据库连接操作的入口 - pd.me ...
- JS的ES7支持
1.指数运算符(幂): ** 2.Array.prototype.includes(value) : 判断数组中是否包含指定value console.log(2**4); let arr = [2, ...
- 10-day03-注释
Python注释 msg = “我爱你中国!” #单行注释使用 ''''''多行注释使用 print(msg) #this code is for >>>> ‘’‘print( ...
- ESA2GJK1DH1K升级篇: STM32远程乒乓升级,基于(GPRS模块AT指令TCP透传方式),定时访问升级(兼容Air202,SIM800)
实现功能概要 单片机定时使用http访问云端的程序版本, 如果版本不一致,然后通过http下载最新的升级文件,实现远程升级STM32. 兼容Air202,SIM800 测试准备工作(默认访问我的服务器 ...
- Koa帮我们做了什么
整理web渲染思路,与KOA作比较 1.开启服务器并监听端口,注册监听事件 // 原生 let http = require('http') const server = http.createSer ...
- python总结七
1.lpython是python语言的lisp前端,它类似于普通的lisp语言,他会被直接编译成字节码. 2.python中的变量只在哪里需要设置内部变量,在__init__函数中. 3.map()会 ...
- [转]MySQL之——崩溃-修复损坏的innodb:innodb_force_recovery
原文 https://blog.csdn.net/l1028386804/article/details/77199194 1. mysql崩溃 --------------------------- ...
- Redis内存回收策略
如果使用Redis的时候,不合理使用内存,把什么东西都放在内存里面,又不设置过期时间,就会导致内存的堆积越来越大.根据28法则,除了20%的热点数据之外,剩余的80%的非热点或不怎么重要的数据都在占用 ...
- AQS2--出队
队列不卡死,一定要:前面节点变成头结点唤醒时候能够唤醒后面节点,依次类推. 设置前面节点=-1就是为了前面节点走的时候,唤醒自己. 正常没有阻塞节点,设置前面=-1,再旋转一次尝试获取锁,才阻塞.即使 ...
- TPad需求和迭代
需求和迭代最主要的区别是什么