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,不可否认,这些类库功能强大,效果也 ...
随机推荐
- python语言(六)mock接口开发、发邮件、写日志、新Excel操作
一.urllib模块 urllib模块是一个标准模块,直接import urllib即可,在python3里面只有urllib模块,在python2里面有urllib模块和urllib2模块. url ...
- javascript中 encodeURIComponent() 与 encodeURI() 的区别
前言:js 中仅有的几个全局函数中,有两个全局函数可以用来编码url 字符串. 一.encodeURIComponent() 将转义用于分隔 URI 各个部分的标点符号 ,也就是可以编码 " ...
- nexus pip proxy config
nexus pip proxy config config for linux touch config touch ~/.pip/pip.conf content [global] index-ur ...
- ABP JS调用接口 获取返回的数据
var _userService = abp.services.app.user; console.log(abp.services.app.user); _userService.getUserBy ...
- 洛谷P3063 [USACO12DEC]牛奶的路由Milk Routing
链接 其实在博客园里写题解都挺应付的都是在洛谷写了之后 挑一部分粘过来 在洛谷写的也都是废话,是为了凑篇幅 主要就是代码 大体思路就一提 这题贪心不行废话 跑m遍SPFA更新最小值 注意数组记得清空 ...
- webpack中的hash、chunkhash、contenthash区别
hash一般是结合CDN缓存来使用,通过webpack构建之后,生成对应文件名自动带上对应的MD5值.如果文件内容改变的话,那么对应文件哈希值也会改变,对应的HTML引用的URL地址也会改变,触发CD ...
- C函数之readlink
函数原型; #include<unistd.h> ssize_t readlink(const char *path, char *buf, size_t bufsiz); 函数说明: r ...
- varnish搭建cdn网络-------3.0.5版本
CDN分发网络CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在 ...
- javaWeb项目配置自定义404错误页
1.情景展示 为了隐藏tomcat版本信息以及显示更友好的错误信息提示,如何将404的错误跳转到指定页面? 2.解决方案 第一步:修改项目的web.xml 将如下代码添加到</web-a ...
- json for modern c++(nlohmann json)使用小计
前言 一开始使用jsoncpp,但是jsoncpp已经不更新了,nlohmann还在更新,并且jsoncpp做过一次大的版本升级,导致api不兼容,以前使用过的工程代码不能很好的升级到新的版本,并且j ...