package com.gys.cyej.utils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import com.gys.cyej.selfview.MyBitmapDrawable;
public class ImageUtil {
/**
* 将图片保存到相应目录中
* @param context
* @param bitmap
* @param fileName
*/
public synchronized static void savaImage(Context context,Bitmap bitmap,String dir,String fileName)
{
File sdDir = new File("/sdcard/");
if (sdDir.exists() && sdDir.canWrite()) {
File uadDir = new File(sdDir.getAbsolutePath() + "/cyej/"+dir);
uadDir.mkdir();
if (uadDir.exists() && uadDir.canWrite()) {
File file = new File(uadDir.getAbsolutePath() + "/" + fileName);
try {
file.createNewFile();
} catch (IOException e) {
Log.e("ImageUtil", "error creating file", e);
}
if (file.exists() && file.canWrite()) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 50, fos);
} catch (FileNotFoundException e) {
Log.e("ImageUtil", "ERROR", e);
} catch (Exception e) {
Log.e("ImageUtil", "ERROR", e);
} finally {
if (fos != null) {
try {
fos.flush();
fos.close();
} catch (IOException e) {
// swallow
}
}
}
} else {
Log.e("ImageUtil", "error writing to file");
}
}
}
recyleBitmapMemory(bitmap);
}
/**
* 从相应目录中获取图片
* @param fileName
* @param context
* @return
*/
public static Bitmap getImage(String dir,String fileName,Context context)
{
File rFile = new File("/sdcard/cyej/"+dir+"/" + fileName);
if (rFile.exists() && rFile.canRead()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(rFile);
BitmapDrawable bd=new BitmapDrawable(fis);
Bitmap newBitmap=bd.getBitmap();
return newBitmap;
} catch (IOException e) {
Log.e("ImageUtil", e.getMessage(), e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// swallow
}
}
}
}
return null;
}
/**
* 从相应目录中获取图片
* @param fileName
* @param context
* @return
*/
public static MyBitmapDrawable getImageDrawable(String dir,String fileName,Context context)
{
File rFile = new File("/sdcard/cyej/"+dir+"/" + fileName);
if (rFile.exists() && rFile.canRead()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(rFile);
MyBitmapDrawable bd=new MyBitmapDrawable(fis);
return bd;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// swallow
}
}
}
}
return null;
}
/**
* 获取相应目录中所有图片
* @param fileName
* @param context
* @return
*/
public static ArrayList<Bitmap> getAllimagefromdir(String dir)
{
ArrayList<Bitmap> listbitmap=new ArrayList<Bitmap>();
File rFile = new File("/sdcard/cyej/"+dir);
String[] filelist=rFile.list();
for(int i=0;i<filelist.length;i++)
{
File readfile = new File(rFile.getAbsolutePath() + "/" + filelist[i]);
if(readfile.getName().indexOf(".txt")==-1)
{
FileInputStream fis = null;
try {
fis = new FileInputStream(readfile);
BitmapDrawable bd=new BitmapDrawable(fis);
Bitmap newBitmap=bd.getBitmap();
listbitmap.add(newBitmap);
} catch (IOException e) {
Log.e("ImageUtil", e.getMessage(), e);
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return listbitmap;
}
/**
* 将图片转换成圆角图片
* @param bitmap
* @return
*/
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
final int color = 0xff424242;
Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 6;
paint.setAntiAlias(true);
paint.setColor(color);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new android.graphics.PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0,0, paint);
return output;
}
/***
* 图片的缩放方法
*
* @param bgimage
* :源图片资源
* @param newWidth
* :缩放后宽度
* @param newHeight
* :缩放后高度
* @return
*/
public static Bitmap zoomImage(Bitmap bgimage) {
int newHeight=0;
if(bgimage.getWidth()>460)
{
newHeight= bgimage.getHeight()/bgimage.getWidth()*460;
}else
{
return bgimage;
}
// 获取这个图片的宽和高
int width = bgimage.getWidth();
int height = bgimage.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) 460) / width;
float scaleHeight = ((float) newHeight) / height;
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
}
/**
* 读取本地图片资源
* @param imgStr 图片路径
* @param context Activity
* @return
*/
public static BitmapDrawable getBitmapDrawable(String imgStr,Context context)
{
Bitmap bmp=null;
try {
bmp=BitmapFactory.decodeStream(context.getAssets().open(imgStr));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable bitmapdraw=new BitmapDrawable(bmp);
return bitmapdraw;
}
/**
* 把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);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
/**
* 将bitmap转换成bytes
* @param bitmap
* @return
*/
public static byte[] bitmapToBytes(Bitmap bitmap) {
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return out.toByteArray();
} catch (IOException e) {
return null;
}
}
/**
* 回收位图所占的空间大小
* @param bitmap
*/
public static void recyleBitmapMemory(Bitmap bitmap)
{
if(null!=bitmap&&!bitmap.isRecycled())
{
bitmap.recycle();
}
}
}