实现给图片添加字体,图片旋转功能: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;
}