package com.smartandroid.sa.bitmap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
public class BitmapInfo {
private Context c;
public BitmapInfo(Context context) {
this.c = context;
}
/**
* get Bitmap from local SDCard��no zoom
*
* @param path
*
* @return Bitmap
*/
public Bitmap getLocalBitmap(String path) {
Bitmap bitmap = null;
try {
File file = new File(path);
if (file.exists()) {
bitmap = BitmapFactory.decodeFile(path);
}
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
/**
* ��BitmapͼƬ����������Ŵ���
*
* @param bm
* Ҫ�����Bitmap����
* @param newWidth
* ������ͼƬ���
* @param newHeight
* ������ͼƬ�߶�
* @return �µ����ź��Bitmap����
*/
public Bitmap getBitmapZoom(Bitmap bm, int newWidth, int newHeight) {
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);
// bm.recycle();
return newBm;
}
/**
* ���ص�ǰBitmapͼƬ�����Ⱥ߶�
*
* @param bm
* Ҫ��ȡ�ߴ��ͼƬBitmap����
* @return Ҫ���ص�ͼƬ�ߴ���Ϣ�����緵�ظ�ʽΪ����ǰͼƬ��Ⱥ߶ȷֱ�Ϊ@700@500
*/
public String getBitmapSize(Bitmap bm) {
int width = bm.getWidth();
int height = bm.getHeight();
String s = "��ǰͼƬ��Ⱥ߶ȷֱ�Ϊ@" + width + "@" + height;
return s;
}
/**
* ��Bitmap���浽ָ��·���Ĭ������Ϊ��ǰʱ�䣬��ʽΪPNG��ע��·����Ҫ��/ </br>
* �������ձ�����Ϊ��/sdcard/cacheImage/2013-08-06_14-57-52.png
*
* @param bm
* Ҫ�����Bitmap����
* @param savePath
* Ҫ������ļ�·����ע��·����Ҫ��/
*
*/
public void saveBitmap(Bitmap bm, String savePath) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String fname = savePath + sdf.format(new Date()) + ".png";
FileOutputStream out;
try {
out = new FileOutputStream(fname);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
System.out.println("file " + fname + "output done.");
bm = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* ��Bitmap���浽ָ����·���£��Լ�����ͼƬ�������ƣ���ʽΪPNG��ʽ
*
* @param bm
* Ҫ�����Bitmap����
* @param savePath
* Ҫ������ļ�·����ע��·����Ҫ��/
* @param saveName
* ������ͼƬ����
*/
public void saveBitmap(Bitmap bm, String savePath, String saveName) {
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String fname = savePath + saveName + ".png";
FileOutputStream out;
try {
out = new FileOutputStream(fname);
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
System.out.println("file " + fname + "output done.");
bm = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* ����Bitmap��ָ�����ļ��У��Լ�ָ���������ƺͱ������ͣ�PNG,JPG,JPEG��
*
* @param bm
* Ҫ�����Bitmap����
* @param savePath
* Ҫ�����·��
* @param saveName
* �������ļ���
* @param imageType
* Ҫ����Ϊ��ͼƬ��ʽ��ֻ�����룺png��jpg��jpeg �������ִ�Сд��
*/
public void saveBitmap(Bitmap bm, String savePath, String saveName, String imageType) {
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String fname = savePath + saveName + "." + imageType;
FileOutputStream out;
try {
out = new FileOutputStream(fname);
if (imageType.trim().toLowerCase().equalsIgnoreCase("png")) {
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
} else if (imageType.trim().toLowerCase().equalsIgnoreCase("jpeg")
|| imageType.trim().toLowerCase().equalsIgnoreCase("jpg")) {
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
}
System.out.println("file " + fname + "output done.");
bm = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* �ȱ�������ͼƬ�������ڶ�������(0.1-1.0)Ϊ��С;(1.1- )Ϊ�Ŵ�
*
* @param bm
* Ҫ��С��Bitmap����
* @param scale
* �ȱ�����С�ijߴ磬��С��Χ��0.1 - 1.0��;�Ŵ�Χ(1.1 - )
* @return �������ź��Bitmap����
*/
public Bitmap getBitmapZoom(Bitmap bm, double scale) {
// if (scale > 1.0) {
// scale = 1.0;
// Toast.makeText(c, "��������ǻ�ȡ��СͼƬ�ķ����������ԷŴ�ͼƬ,�뽲������ΪС�ڻ����1.0", 3000)
// .show();
// }
int width = bm.getWidth();
int height = bm.getHeight();
// ȡ����Ҫ���ŵ�matrix����
Matrix matrix = new Matrix();
matrix.postScale((float) scale, (float) scale);
Bitmap newBm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
System.out.println("��ͼƬ�ijߴ磺" + newBm.getWidth() + "," + newBm.getHeight());
return newBm;
}
/**
* ָ��ͼƬ·������ͼƬ���еȱ�����С��ָ��Ҫ��С����ͼƬ��ȣ�ͼƬ�߶ȵȱ����Զ���С
*
* @param picPath
* Ҫ�����ͼƬ��·��
* @param height
* Ҫ��С����ͼƬ���
* @return Bitmap����
*/
public Bitmap getBitmapZoom(String picPath, double height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bitmap = BitmapFactory.decodeFile(picPath, options); // ��ʱ����bmΪ��
options.inJustDecodeBounds = false;
// ���űȡ������ǹ̶��������ţ�ֻ�ø��߿�����һ�����ݽ��м��㼴��
int be = (int) (options.outHeight / (float) height);
if (be <= 0)
be = 1;
options.inSampleSize = be;
// ���¶���ͼƬ��ע���ʱ�Ѿ���options.inJustDecodeBounds ���false��
bitmap = BitmapFactory.decodeFile(picPath, options);
int w = bitmap.getWidth();
int h = bitmap.getHeight();
return bitmap;
}
/**
* ���ñ���ͼƬ�����ķ�����Ĭ�ϱ���Ϊ.jpg��ʽ�ļ�,��СͼƬ��С���ļ���Ϊʱ���ʽ
*
* @param bm
* Ҫ�����ͼƬ
* @param quality
* Ҫ�����ͼƬ������0-100��ֵԽ��ѹ����Խ�ߣ���ѹ������Ϊ100
*/
public void saveBitmapQuality(Bitmap bm, int quality, String savePath) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US);
File file = new File(savePath);
if (!file.exists()) {
file.mkdir();
}
String fname = savePath + sdf.format(new Date()) + ".jpg";
FileOutputStream out;
try {
out = new FileOutputStream(fname);
bm.compress(Bitmap.CompressFormat.JPEG, quality, out);
System.out.println("file " + fname + "output done.");
bm = null;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* ��Bitmap����ת��ΪDrawable����
*
* @param bitmap
* Ҫת����Bitmap
* @return ����ת�����Drawable����
*/
public Drawable bitmapToDrawable(Bitmap bitmap) {
Drawable drawable = new BitmapDrawable(bitmap);
return drawable;
}
/**
* byte[]����תΪ Bitmap
*
* @param b
* ��������
* @return ��byte[]����ת��Ϊ��Bitmap����
*/
public Bitmap bytesToBimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
/**
* Bitmap ת byte[]
*
* @param bm
* bitmap����
* @return ����ת��Bitmapת�������������
*/
public byte[] bitmapToBytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 70, baos);
return baos.toByteArray();
}
}