這些天一直爲android程序如何壓縮圖片煩惱,上網找了不少資料,整理了一下,通過測試,都是可用的,android
/** * 壓縮圖片 * @param bitmap 源圖片 * @param width 想要的寬度 * @param height 想要的高度 * @param isAdjust 是否自動調整尺寸, true圖片就不會拉伸,false嚴格按照你的尺寸壓縮 * @return Bitmap */ public Bitmap reduce(Bitmap bitmap, int width, int height, boolean isAdjust) { // 若是想要的寬度和高度都比源圖片小,就不壓縮了,直接返回原圖 if (bitmap.getWidth() < width && bitmap.getHeight() < height) {return bitmap;} // 根據想要的尺寸精確計算壓縮比例, 方法詳解:public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode); // scale表示要保留的小數位, roundingMode表示如何處理多餘的小數位,BigDecimal.ROUND_DOWN表示自動捨棄 float sx = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()), 4, BigDecimal.ROUND_DOWN).floatValue(); float sy = new BigDecimal(height).divide(new BigDecimal(bitmap.getHeight()), 4, BigDecimal.ROUND_DOWN).floatValue(); if (isAdjust) {// 若是想自動調整比例,不至於圖片會拉伸 sx = (sx < sy ? sx : sy);sy = sx;// 哪一個比例小一點,就用哪一個比例 } Matrix matrix = new Matrix(); matrix.postScale(sx, sy);// 調用api中的方法進行壓縮,就大功告成了 return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
旋轉圖片:canvas
/** * 旋轉圖片 * @param bitmap 源圖片 * @param angle 旋轉角度(90爲順時針旋轉,-90爲逆時針旋轉) * @return Bitmap */ public Bitmap rotate(Bitmap bitmap, float angle) { Matrix matrix = new Matrix(); matrix.postRotate(angle); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); }
放大或縮小圖片:api
/** * 放大或縮小圖片 * @param bitmap 源圖片 * @param ratio 放大或縮小的倍數,大於1表示放大,小於1表示縮小 * @return Bitmap */ public Bitmap zoom(Bitmap bitmap, float ratio) { if (ratio < 0f) {return bitmap;} Matrix matrix = new Matrix(); matrix.postScale(ratio, ratio); return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); }
在圖片上印字:dom
/** * 在圖片上印字 * @param bitmap 源圖片 * @param text 印上去的字 * @param param 字體參數分別爲:顏色,大小,是否加粗,起點x,起點y; 好比:{color : 0xFF000000, size : 30, bold : true, x : 20, y : 20} * @return Bitmap */ public Bitmap printWord(Bitmap bitmap, String text, Map<String, Object> param) { if (ToolUtil.get().isBlank(text) || null == param) {return bitmap;} Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); canvas.drawBitmap(bitmap, 0, 0, null);canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore(); Paint paint = new Paint(); paint.setColor(null != param.get("color") ? (Integer) param.get("color") : Color.BLACK); paint.setTextSize(null != param.get("size") ? (Integer) param.get("size") : 20); paint.setFakeBoldText(null != param.get("bold") ? (Boolean) param.get("bold") : false); canvas.drawText(text, null != param.get("x") ? (Integer) param.get("x") : 0, null != param.get("y") ? (Integer) param.get("y") : 0, paint); canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore(); return newBitmap; }
建立logo(給圖片加水印), :ide
/** * 建立logo(給圖片加水印), * @param bitmaps 原圖片和水印圖片 * @param left 左邊起點座標 * @param top 頂部起點座標t * @return Bitmap */ public Bitmap createLogo(Bitmap[] bitmaps, int left, int top) { Bitmap newBitmap = Bitmap.createBitmap(bitmaps[0].getWidth(), bitmaps[0].getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(newBitmap); for (int i = 0; i < bitmaps.length; i++) { if (i == 0) { canvas.drawBitmap(bitmaps[0], 0, 0, null); } else { canvas.drawBitmap(bitmaps[i], left, top, null); } canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore(); } return newBitmap; }
產生一個4位隨機數字的圖片驗證碼:post
private int width = 140, height = 40, codeLen = 4; private String checkCode = ""; private Random random = new Random(); /** * 產生一個4位隨機數字的圖片驗證碼 * @return Bitmap */ public Bitmap createCode() { checkCode = ""; String[] chars = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }; for (int i = 0; i < codeLen; i++) {checkCode += chars[random.nextInt(chars.length)];} Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888); Canvas canvas = new Canvas(bitmap);canvas.drawColor(Color.WHITE); Paint paint = new Paint();paint.setTextSize(30);paint.setColor(Color.BLUE); for (int i = 0; i < checkCode.length(); i++) { paint.setColor(randomColor(1));paint.setFakeBoldText(random.nextBoolean()); float skewX = random.nextInt(11) / 10; paint.setTextSkewX(random.nextBoolean() ? skewX : -skewX); int x = width / codeLen * i + random.nextInt(10); canvas.drawText(String.valueOf(checkCode.charAt(i)), x, 28, paint); } for (int i = 0; i < 3; i++) {drawLine(canvas, paint);} for (int i = 0; i < 255; i++) {drawPoints(canvas, paint);} canvas.save(Canvas.ALL_SAVE_FLAG);canvas.restore(); return bitmap; } /** * 得到一個隨機的顏色 * @param rate * @return */ public int randomColor(int rate) { int red = random.nextInt(256) / rate, green = random.nextInt(256) / rate, blue = random.nextInt(256) / rate; return Color.rgb(red, green, blue); } /** * 畫隨機線條 * @param canvas * @param paint */ public void drawLine(Canvas canvas, Paint paint) { int startX = random.nextInt(width), startY = random.nextInt(height); int stopX = random.nextInt(width), stopY = random.nextInt(height); paint.setStrokeWidth(1);paint.setColor(randomColor(1)); canvas.drawLine(startX, startY, stopX, stopY, paint); } /** * 畫隨機干擾點 * @param canvas * @param paint */ public void drawPoints(Canvas canvas, Paint paint) { int stopX = random.nextInt(width), stopY = random.nextInt(height); paint.setStrokeWidth(1); paint.setColor(randomColor(1)); canvas.drawPoint(stopX, stopY, paint); } /** * 返回真實驗證碼字符串 * @return String */ public String getCheckCode() { return checkCode; }