**....图挂了f**k
---
不多说
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.ExifInterface;
import android.os.Environment;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.Random;
public class BitmapTools {
public boolean saveBitmap2File(Bitmap bmp, File filename) {
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
public static String sdcardCameraCacheDir(Context context) {
String strDir = BitmapTools.getCacheFile(context) + "/camera_tmp/";
File dir = new File(strDir);
if (!dir.exists())
dir.mkdirs();
return strDir;
}
public static File getCacheFile(Context context) {
if (context != null) {
File dir = context.getExternalCacheDir();
if (dir == null) {
dir = context.getCacheDir();
}
return dir;
}
return Environment.getExternalStorageDirectory();
}
public Bitmap getBitmapFromFile(File filename) {
return BitmapFactory.decodeFile(filename.getPath());
}
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);
}
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);
}
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);
}
public int readPictureDegree(String path) {
int degree = 0;
try {
ExifInterface exifInterface = new ExifInterface(path);
int orientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
degree = 90;
break;
case ExifInterface.ORIENTATION_ROTATE_180:
degree = 180;
break;
case ExifInterface.ORIENTATION_ROTATE_270:
degree = 270;
break;
}
} catch (IOException e) {
e.printStackTrace();
}
return degree;
}
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;
}
private int width = 140, height = 40, codeLen = 4;
private String checkCode = "";
private Random random = new Random();
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;
}
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);
}
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);
}
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);
}
public String getCheckCode() {
return checkCode;
}
}