實現給圖片添加字體,圖片旋轉功能: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;
}