/** * @author Guan * @file com.guan.o2o.utils * @date 2015/9/22 * @Version 1.0 */
public class BitmapUtil {
/** * 将图片存储到sdcard中 * * @param bitmap * @param imageName * @param imageFilePath */
public static void storeImageToSDCARD(Bitmap bitmap, String imageName,
String imageFilePath) {
File file = new File(imageFilePath);
if (!file.exists()) {
file.mkdir();
}
File imagefile = new File(file, imageName + ".jpg");
try {
imagefile.createNewFile();
FileOutputStream fos = new FileOutputStream(imagefile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/** * sdcard取图片 * * @param imageFilePath * @param picName * @return */
public static Bitmap getBitmapBySDCARD(String imageFilePath, String picName) {
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
return null;
}
try {
File file = new File(imageFilePath, picName + ".jpg");
FileInputStream inputStream = new FileInputStream(file);
byte[] b = new byte[inputStream.available()];
inputStream.read(b);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return bitmap;
} catch (Exception e) {
return null;
}
}
/** * 以最省内存的方式读取本地资源的图片 * * @param context * @param drawableId * @param screenWidth * @param screenHight * @return */
public static Bitmap readBitmapById(Context context, int drawableId,
int screenWidth, int screenHight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inInputShareable = true;
options.inPurgeable = true;
InputStream stream = context.getResources().openRawResource(drawableId);
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
return getBitmap(bitmap, screenWidth, screenHight);
}
/** * 等比例压缩图片 * * @param bitmap * @param screenWidth * @param screenHight * @return */
public static Bitmap getBitmap(Bitmap bitmap, int screenWidth,
int screenHight) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scale = (float) screenWidth / w;
float scale2 = (float) screenHight / h;
scale = scale < scale2 ? scale : scale2;
matrix.postScale(scale, scale);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
}
/** * 将Drawable转为Bitmap * @param drawable * @return */
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
/** * 将Bitmap转为btye[] * @param bmp * @param needRecycle * @return */
public static byte[] bmpToByteArray(final Bitmap bmp,
final boolean needRecycle) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 80, output);
if (needRecycle) {
bmp.recycle();
}
byte[] result = output.toByteArray();
try {
output.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/** * 网络取图片 getImage (inputStream –> byte –> bitmap)(最佳方法) * @param path * @return * @throws Exception */
public Bitmap getImage(String path) throws Exception {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(10 * 1000);
conn.setConnectTimeout(10 * 1000);
conn.setRequestMethod("GET");
InputStream in = null;
if(conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
in = conn.getInputStream();
} else {
in = null;
}
if (in == null){
throw new RuntimeException("stream is null");
} else {
try {
byte[] data = getBytes(in);
if(data!=null){
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
in.close();
return null;
}
}
/** * 在getImage()中调用 * * 得到图片字节流数组大小 inputStream --> byte * @param inStream * @return * @throws Exception */
public static byte[] getBytes(InputStream inStream) throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
outStream.close();
inStream.close();
return outStream.toByteArray();
}
/** * 网络取图片 inputStream --> drawable * @param imageUrl * @return */
private Drawable loadDrawableImage(String imageUrl) {
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.jpg");
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return drawable;
}
/** * 将两个bitmap对象整合并保存为一张图片 * @param background * @param foreground * @return */
public Bitmap combineBitmap(Bitmap background, Bitmap foreground) {
int bgWidth = background.getWidth();
int bgHeight = background.getHeight();
int fgWidth = foreground.getWidth();
int fgHeight = foreground.getHeight();
Bitmap newmap = Bitmap.createBitmap(bgWidth, bgHeight + fgHeight,
android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newmap);
canvas.drawBitmap(background, 0, 0, null);
canvas.drawBitmap(foreground, 0, bgHeight, null);
return newmap;
}
/** * Bitmap旋转一定角度 * @param b * @param degrees * @return */
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees, (float) b.getWidth() / 2,
(float) b.getHeight() / 2);
try {
Bitmap b2 = Bitmap.createBitmap(b, 0, 0, b.getWidth(),
b.getHeight(), m, true);
return b2;
} catch (OutOfMemoryError ex) {
return b;
} finally {
b.recycle();
}
}
return b;
}
/** * Bitmap画一个圆角图 * @param bitmap * @param roundPx * @return */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
if (bitmap == null) {
return bitmap;
}
try {
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(),
bitmap.getHeight());
final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
} catch (OutOfMemoryError e) {
System.gc();
return null;
}
}
/** * 从view 得到图片 * @param view * @return */
public static Bitmap getBitmapFromView(View view) {
view.destroyDrawingCache();
view.measure(View.MeasureSpec.makeMeasureSpec(0,
View.MeasureSpec.UNSPECIFIED), View.MeasureSpec
.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.setDrawingCacheEnabled(true);
Bitmap bitmap = view.getDrawingCache(true);
return bitmap;
}
/** * 添加水印 * @param src * @param watermark * @param title * @return */
public static Bitmap watermarkBitmap(Bitmap src, Bitmap watermark,
String title) {
if (src == null) {
return null;
}
int w = src.getWidth();
int h = src.getHeight();
Bitmap newb = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas cv = new Canvas(newb);
cv.drawBitmap(src, 0, 0, null);
Paint paint = new Paint();
if (watermark != null) {
int ww = watermark.getWidth();
int wh = watermark.getHeight();
paint.setAlpha(50);
cv.drawBitmap(watermark, w - ww + 5, h - wh + 5, paint);
}
if (title != null) {
String familyName = "宋体";
Typeface font = Typeface.create(familyName, Typeface.BOLD);
TextPaint textPaint = new TextPaint();
textPaint.setColor(Color.RED);
textPaint.setTypeface(font);
textPaint.setTextSize(22);
StaticLayout layout = new StaticLayout(title, textPaint, w,
Layout.Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
layout.draw(cv);
}
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
return newb;
}
}