/**
* 图片工具类
*
*
* @author minner
*
*/
public class ImageUtils {
// 将Drawbale图片转换为Bitmap位图
// Resources resources = getResources();
// Bitmap bitmap = BitmapFactory.decodeResource(resources,
// R.drawable.image_gb);
// 切图为圆角调用
// setImageBitmap(removeYuanjiao(bitmap, 5));
// 去灰色
// setImageBitmap(removeHuiDu(bitmap));
// 去灰色并圆角
// setImageBitmap(HuiDuYuanJiao(bitmap,5));
// 切图为圆角
public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels) {
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Bitmap creBitmap = Bitmap.createBitmap(width, height,
android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(creBitmap);
Paint paint = new Paint();
RectF rectF = new RectF(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, pixels, pixels, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
return creBitmap;
} else {
return null;
}
}
/**
*
*
* 图片缩小
*
* @param bitmap
* @param w
* 缩小后的宽
* @param h
* 缩小后的高
* @return
*/
public static Bitmap resizeImage(Bitmap bitmap, int w, int h) {
Bitmap BitmapOrg = bitmap;
int width = BitmapOrg.getWidth();
int height = BitmapOrg.getHeight();
int newWidth = w;
int newHeight = h;
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
height, matrix, true);
return resizedBitmap;
}
/**
*
*
*
* @param bitmap
* 源图像
* @param pixels
* 圆角比例 默认5
* @param ratio
* 比例
* @return
*/
public static Bitmap removeYuanjiao(Bitmap bitmap, int pixels, int ratio) {
if (bitmap != null) {
int width = bitmap.getWidth() / ratio;
int height = bitmap.getHeight() / ratio;
Bitmap creBitmap = Bitmap.createBitmap(width, height,
android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(creBitmap);
Paint paint = new Paint();
RectF rectF = new RectF(0, 0, width, height);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
canvas.drawRoundRect(rectF, pixels, pixels, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, 0, 0, paint);
return creBitmap;
} else {
return null;
}
}
// 将图片去灰色
public static Bitmap removeHuiDu(Bitmap bitmap) {
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
System.out.println("width--------------" + width
+ "------------height-----" + height);
Bitmap creBitmap = Bitmap.createBitmap(width, height,
android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(creBitmap);
Paint paint = new Paint();
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setSaturation(0);
ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(
colorMatrix);
paint.setColorFilter(colorMatrixFilter);
canvas.drawBitmap(bitmap, 0, 0, paint);
System.out.println("123width--------------" + width
+ "------------height-----" + height);
return creBitmap;
} else {
return null;
}
}
/**
* 根据名字获取圆角图片
*
* @param path
* 图片路径
* @return bitmap 图片对象
*/
public static Drawable getBitmapByDrawable(Drawable drawable) {
BitmapDrawable bitmap1 = null;
// 捕获OOM异常
try {
BitmapDrawable bd=(BitmapDrawable)drawable;
//
bitmap = ImageUtil.removeHuiDu(ImageUtil.removeYuanjiao(bd.getBitmap(), 20));
Bitmap bitmap =removeYuanjiao(bd.getBitmap(),10);
bitmap1 = new BitmapDrawable(bitmap);
} catch (OutOfMemoryError e) {
e.printStackTrace();
}
if (bitmap1 == null) {
return null;
}
return bitmap1;
}
// 去灰色圆角
public static Bitmap HuiDuYuanJiao(Bitmap bitmap, int pixels) {
return removeYuanjiao(removeHuiDu(bitmap), pixels);
}
}
-----------------------------------------数据库处理类-(存储单个,多个图片)------------------------------------------
/**
* 数据库操作类,封装增、删、改、查等操作。
*/
public class DBService {
public static DBService instance;
private DBHelper dbHelper;
private SQLiteDatabase database;
private Context context;
public DBService(Context context) {
this.context = context;
dbHelper = new DBHelper(context);
}
public static synchronized DBService getInstance(Context context) {
if (instance == null) {
instance = new DBService(context);
}
instance.openWriteable();
return instance;
}
/**
* 读写方式打开数据库,一旦数据库的磁盘空间满了,数据库就只能读而不能写, <br>
* getWritableDatabase()打开数据库就会出错
*
* @throws SQLException
*/
public void openWriteable() throws SQLException {
database = dbHelper.getWritableDatabase();
}
/**
* 关闭数据库服务
*/
public void close() {
if (dbHelper != null) {
dbHelper.close();
dbHelper = null;
instance = null;
}
}
/**
* /**
*
* 存入图片
*
* @param bmp
* 图片
* @param ImageName
* 图片名字
* @return 存储是否成功
*/
public boolean insertImage(Bitmap bmp, String ImageName) {
boolean tag = false;
ContentValues initialValues = new ContentValues();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
initialValues.put(DBHelper.IMG_NAME, ImageName);
initialValues.put(DBHelper.IMG_DATA, os.toByteArray());
long sum = database
.insert(DBHelper.IMG_TABLE_NAME, null, initialValues);
;
if (sum != -1) {
tag = true;
}
System.out.println(DBHelper.IMG_TABLE_NAME + "存入图片---" + ImageName
+ tag);
database.close();
return tag;
}
/**
* 获取某张图片
*
* @param cust_id
* 微购号
*
* @return
*/
public Bitmap getOneImg(String imgName) {
Cursor c = database.query(DBHelper.IMG_TABLE_NAME, null,
DBHelper.IMG_NAME + "='" + imgName + "'", null, null, null,
null);
Bitmap bitmap = null;
if (c != null && c.getCount() != 0) {// 数据库中有数据
c.moveToFirst();
byte[] in = c.getBlob(c.getColumnIndex(DBHelper.IMG_DATA));
bitmap = BitmapFactory.decodeByteArray(in, 0, in.length);
System.out.println("----------读取图片----" + imgName);
}
if (c != null) {
c.close();
c = null;
}
return bitmap;
}
}
------------------------------------------------数据库表的创建类---------------------------------------
/**
*数据库创建类
*/
public class DBHelper extends SQLiteOpenHelper {
public static String Lock = "dblock";
public static String file_Lock = "fileLock";
public static final String DATABASE_NAME = "modem";
public static final int DATABASE_VERSION = 2;
public static final String CTEATE_TABLE = " CREATE TABLE IF NOT EXISTS ";
public static final String DROP_TABLE = " DROP TABLE IF EXISTS ";
public static final String TABLE_ID = "_id";
public static final String PRIMARY_KEY = " integer primary key autoincrement, ";
public static final String OWNER_ID = " ownerid ";
// 图片表各字段
public static final String IMG_TABLE_NAME = "imgTable";//图片存储表名
public static final String IMG_NAME = "img_name";//图片保存名称
public static final String IMG_DATA = "img_data";//图片存储数据体
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(createImageSave());
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE + IMG_TABLE_NAME);
onCreate(db);
}
private String createImageSave() {
System.out.println("创建保存头像图片表 ");
StringBuffer sb = new StringBuffer();
sb.append(CTEATE_TABLE).append(IMG_TABLE_NAME);
sb.append("(");
sb.append(TABLE_ID).append(PRIMARY_KEY);
sb.append(IMG_DATA).append(" BLOB, ");
sb.append(IMG_NAME).append(" TEXT ");
sb.append(")");
System.out.println("\n createMsgSQL--->" + sb.toString());
return sb.toString();
}
}