package com.smartdot.cgt.util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; public class BitmapConvert { /** * 根据源图片文件进行缩放并读取至内存 * * @param sourceFileName * @return */ public static Bitmap resizeBitmap(String sourceFileName) { int toSize = ApplicationMain.getInstance().getCgtConfig().getUploadImageLength(); Bitmap resizedBitmap = null; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(sourceFileName, options); options.inJustDecodeBounds = false; int width = options.outWidth; int height = options.outHeight; if (width > toSize || height > toSize) { float temp = ((float) height) / ((float) width); int newHeight = 0, newWidth = 0; if (temp > 1) { newHeight = toSize; newWidth = (int) (newHeight / temp); } else { newWidth = toSize; newHeight = (int) (newWidth * temp); } float scaleWidth = ((float) width) / newWidth; int i = 1; while (true) { if (scaleWidth < Math.pow(2, i)) { double left = scaleWidth - Math.pow(2, i - 1); double right = Math.pow(2, i) - scaleWidth; options.inSampleSize = (left > right) ? (int) Math.pow(2, i) : (int) Math.pow(2, i - 1); break; } i++; } } else { options.inSampleSize = 1; } FileInputStream is = null; try { is = new FileInputStream(sourceFileName); resizedBitmap = BitmapFactory.decodeStream(is, null, options).copy(Bitmap.Config.ARGB_8888, true); SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf2 = new SimpleDateFormat("HH:mm:ss"); Date date = new Date(); Paint paint = new Paint(); paint.setARGB(255, 255, 20, 20); paint.setTextSize(20); paint.setAntiAlias(true); paint.setTextAlign(Paint.Align.RIGHT); float textWidth=paint.measureText("a"); Canvas canvas = new Canvas(resizedBitmap); // 画日期 canvas.drawText(sdf1.format(date), resizedBitmap.getWidth() - textWidth, resizedBitmap.getHeight() - textWidth*3, paint); // 画时间 canvas.drawText(sdf2.format(date), resizedBitmap.getWidth() - textWidth, resizedBitmap.getHeight() - textWidth*1, paint); String userName = "", userId = ""; if (ApplicationMain.getInstance().getUserModel() != null) { userId = ApplicationMain.getInstance().getUserModel().getUserId(); // userName = ApplicationMain.getInstance().getUserModel().getUserName(); } // 画用户信息 canvas.drawText(userName + "(" + userId + ")", resizedBitmap.getWidth() - textWidth, resizedBitmap.getHeight() - textWidth*5, paint); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { } } } return resizedBitmap; } }