package com.jqyd.app;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
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.Typeface;
import android.graphics.Bitmap.Config;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Audio.Media;
import android.text.format.Time;
import com.jqyd.camera.PhotoUtil;
import com.jqyd.shareInterface.Optsharepre_interface;
public class BitmapUtils {
private Context context;
private Optsharepre_interface share_obj = null;
private ShareMethod share;
public BitmapUtils (Context context){
this.context=context;
share_obj = new Optsharepre_interface(context);
share = new ShareMethod(context);
}
//读取图片到bitmap中
public Bitmap decodeBitmap(String imagePath) {
try{
File file = new File(imagePath);
if(file.exists()){
share.recordLog("图片存在");
}else{
share.recordLog("图片不存在");
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
// System.out.println("bitmap:"+bitmap.getHeight()+" "+bitmap.getWidth());
share.recordLog("生成的图片为::"+bitmap);
return bitmap;
}catch(Exception e){
e.printStackTrace();
share.recordLog(e.getClass().toString()+e.getCause()+e.getMessage());
}
return null;
}
/**
*
* Description: 压缩图片到特定的长宽
* Title: zoomBitmap
* @param bm
* @param newWidth
* @param newHeight
* @return
* Bitmap
*/
public Bitmap zoomBitmap(Bitmap bm, int newWidth, int newHeight) {
// 图片源
// Bitmap bm = BitmapFactory.decodeStream(getResources()
// .openRawResource(id));
// 获得图片的宽高
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
int w = newbm.getWidth();
int h = newbm.getHeight();
System.out.println("压缩后图片高度:" + h+"压缩后图片宽度:" + w);
//Height.setText("压缩后图片高度:" + h);
// Width.setText("压缩后图片宽度:" + w);
return newbm;
}
/**
*
* Description: 根据配置文件得到精确的压缩文件
* Title: zoomBitmap
* @param bitmap
* @return
* Bitmap
*/
public Bitmap zoomBitmap(Bitmap bitmap) {
Bitmap bitmap_new = null;
int maxLen = 0;
int mark = 0;
double scale = 1;
mark = bitmap.getHeight()>bitmap.getWidth()? 1:2;
switch(mark){
case 1:
if(bitmap.getHeight() > 1024){
scale = 1024.0 /bitmap.getHeight();
}
bitmap_new = zoomBitmap(bitmap, (int)(bitmap.getWidth() * scale), 1024);
break;
case 2:
if(bitmap.getWidth() >1024){
scale = 1024.0 /bitmap.getWidth();
}
bitmap_new = zoomBitmap(bitmap,1024 , (int)(bitmap.getHeight() * scale));
break;
}
return createBitmap(bitmap_new);
}
// 给图片添加水印
public Bitmap createBitmap(Bitmap src) {
Time t = new Time();
t.setToNow();
int w = src.getWidth();
int h = src.getHeight();
String mstrTitle = t.year+"-"+(t.month+1)+"-"+t.monthDay+" "+t.hour + ":" + t.minute + ":" + t.second;
Bitmap bmpTemp = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(bmpTemp);
Paint p = new Paint();
String familyName = "宋体";
Typeface font = Typeface.create(familyName, Typeface.BOLD);
p.setColor(Color.GRAY);
p.setTypeface(font);
p.setTextSize((int)(w*0.08));
canvas.drawBitmap(src, 0, 0, p);
canvas.drawText(mstrTitle, w*1/6,h*7/8, p);
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
return bmpTemp;
}
/**
*
* Description: 保存图片
* Title: saveBitmap
* @param bitmap
* @throws IOException
* void
*/
public String saveBitmap(Bitmap bitmap) throws IOException {
String imagePath_new="";//图片保存路径
SimpleDateFormat timeStampFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
String filename = timeStampFormat.format(new Date());
ContentValues values = new ContentValues();
values.put(Media.TITLE, filename);
Uri photoUri = null;
boolean isOldPath=true;
try{
photoUri = context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if(photoUri==null){
// return "0";
}
}catch(Exception e){
isOldPath = false;
e.printStackTrace();
}
if(isOldPath&&photoUri!=null){
imagePath_new = getRealPathFromURI(photoUri, context.getContentResolver());
}else{
imagePath_new = PhotoUtil.SDCARD_ROOT_PATH+ PhotoUtil.SAVE_PATH_IN_SDCARD+PhotoUtil.createPhotoName();
}
File file = new File(imagePath_new);
System.out.println("路径: " + imagePath_new);
FileOutputStream out;
try{
out = new FileOutputStream(file);
if(bitmap.compress(Bitmap.CompressFormat.JPEG, 80, out)) {
out.flush();
out.close();
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
if(new File(imagePath_new).length()==0){
//判断图片是否保存
imagePath_new="";
}
return imagePath_new;
}
// 获得图片保存的路径
public String getRealPathFromURI(Uri uri, ContentResolver resolver) {
String str;
try {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = resolver.query(uri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
str = cursor.getString(column_index);
cursor.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
str = PhotoUtil.SDCARD_ROOT_PATH+ PhotoUtil.SAVE_PATH_IN_SDCARD+PhotoUtil.createPhotoName();
}
return str;
}
/**
*
* Description: 将图片缩放到原来的四分之一
* Title: decodeBitmap_none
* @param imagePath
* @return
* Bitmap
*/
public Bitmap decodeBitmap_none(String imagePath) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);
if (bitmap == null)
{
System.out.println("bitmap为空");
}
int realWidth = options.outWidth;
int realHeight = options.outHeight;
File file = new File(imagePath);
String realSize = FormetFileSize(file.length());
//RealHeight.setText("真实图片高度:" + realHeight);
//RealWidth.setText("真实图片宽度:" + realWidth);
//RealSize.setText("真实图片大小:" + realSize);
// RealPath.setText("真实图片路径:" + imagePath);
options.inSampleSize = 4;
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(imagePath, options);
return bitmap;
}
/*** 转换文件大小单位(b/kb/mb/gb) ***/
public String FormetFileSize(long fileS) {// 转换文件大小
DecimalFormat df = new DecimalFormat("#.00");
String fileSizeString = "";
if (fileS < 1024) {
fileSizeString = df.format((double) fileS) + "B";
} else if (fileS < 1048576) {
fileSizeString = df.format((double) fileS / 1024) + "K";
} else if (fileS < 1073741824) {
fileSizeString = df.format((double) fileS / 1048576) + "M";
} else {
fileSizeString = df.format((double) fileS / 1073741824) + "G";
}
return fileSizeString;
}
}