实现给图片添加字体,图片旋转功能: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-----实现给图片添加字体的更多相关文章

  1. Android控件上添加图片

    项目中有一个点赞功能,点赞的小图标添加在点赞列表旁边,在xml里可以进行设置,也可以在代码中进行绘图. 下面是两种方法的设置: 1.xml里:一些控件:button.textView等等里面有个属性是 ...

  2. android图像处理系列之四-- 给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  3. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  4. Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等

    仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...

  5. android获得ImageView图片的等级

    android获得ImageView图片的等级问题 要实现的功能如下图,点击分享能显示选中与不选中状态,然后发送是根据状态来实现具体分享功能. 在gridview中有5个子项,每个子元素都有两张图片A ...

  6. ios图片添加文字或者水印

    在项目中,我们会对图片做一些处理,但是我们要记住,一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问题.首先让我们来看看在图片上添加文字的方法 ...

  7. linux服务器下添加字体

    版权声明:本文为楼主原创文章,未经楼主允许不得转载,如要转载请注明来源. 引言:这两天在开发一个动态生成海报的东西(图片拼接,图片水印),开发在windows下没有问题,图片和文字都能正常的生成出来. ...

  8. php 图片添加文字水印 以及 图片合成(微信快码传播)

    1.图片添加文字水印: $bigImgPath = 'backgroud.png'; $img = imagecreatefromstring(file_get_contents($bigImgPat ...

  9. 13、在 uwp应用中,给图片添加高斯模糊滤镜效果(一)

    如果在应用中,如果想要给app 添加模糊滤镜,可能第一想到的是第三方类库,比如 Win2d.lumia Imaging SDK .WriteableBitmapEx,不可否认,这些类库功能强大,效果也 ...

随机推荐

  1. java类uuid源码分析

    通用唯一识别码(英语:Universally Unique Identifier,简称UUID)是一种软件建构的标准,亦为自由软件基金会组织在分散式计算环境领域的一部份.UUID的目的,是让分散式系统 ...

  2. table开发中遇到的问题

    table元素是一个很常用的元素.但是在开发中,我也遇到了一些值得记录下来的问题及解决方案: 1.td内容溢出时,隐藏内容并且以省略号提示 .hide-content{ /* 不换行 */ white ...

  3. 解决github release下载慢/下载失败的问题

    在使用github时,有时作者会在release中提供编译好的程序,以https://github.com/AkikoZ/alfred-web-search-suggest为例,是一个alfred3的 ...

  4. SpringData JPA实现增删改查

    application.properties配置 一.创建实体类并自动生成数据库表 二.dao层继承JpaRepository 三.controller中增加操作 结果: 删除操作: 修改操作:

  5. [Gamma阶段]测试报告

    [Gamma阶段]测试报告 博客目录 测试方法及过程 在正式发布前,为检验后端各接口功能的正确性,后端服务器对压力的耐受程度,以及前端各页面.功能的运行情况,我们对我们的服务器及小程序进行了多种测试. ...

  6. c语言之连接符

    c语言之连接符 1.连接符 连接符的概念是结合define预编译指令的使用技巧,用户可以向define中传入字符串来调用不同功能的函数. 2.代码例子 #include <stdio.h> ...

  7. elasticsearch容量规划

    https://docs.bonsai.io/article/123-capacity-planning Capacity Planning Capacity planning is the proc ...

  8. sql实现MD5加密

    select substring(sys.fn_sqlvarbasetostr(HashBytes('MD5','test')),3,32)

  9. NET Core3前后端分离开发框架

    NET Core前后端分离快速开发框架 https://www.cnblogs.com/coldairarrow/p/11870993.html 引言 时间真快,转眼今年又要过去了.回想今年,依次开源 ...

  10. 【转帖】普通程序员如何转向AI方向

    普通程序员如何转向AI方向 https://www.cnblogs.com/subconscious/p/6240151.html 眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智 ...