import javaioByteArrayInputStream;
import javaioByteArrayOutputStream;
import javaioFile;
import javaioFileNotFoundException;
import javaioFileOutputStream;
import javaioIOException;
import androidgraphicsBitmap;
import androidgraphicsBitmapConfig;
import androidgraphicsBitmapFactory;
/**
* Image compress factory class
*
* @author
*
*/
public class ImageFactory {
/**
* Get bitmap from specified image path
*
* @param imgPath
* @return
*/
public Bitmap getBitmap(String imgPath) {
// Get bitmap through image path
BitmapFactoryOptions newOpts = new BitmapFactoryOptions();
newOptsinJustDecodeBounds = false;
newOptsinPurgeable = true;
newOptsinInputShareable = true;
// Do not compress
newOptsinSampleSize = 1;
newOptsinPreferredConfig = ConfigRGB_565;
return BitmapFactorydecodeFile(imgPath, newOpts);
}
/**
* Store bitmap into specified image path
*
* @param bitmap
* @param outPath
* @throws FileNotFoundException
*/
public void storeImage(Bitmap bitmap, String outPath) throws FileNotFoundException {
FileOutputStream os = new FileOutputStream(outPath);
bitmapcompress(BitmapCompressFormatJPEG, 100, os);
}
/**
* Compress image by pixel, this will modify image width/height
* Used to get thumbnail
*
* @param imgPath image path
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public Bitmap ratio(String imgPath, float pixelW, float pixelH) {
BitmapFactoryOptions newOpts = new BitmapFactoryOptions();
// 开始读入图片,此时把optionsinJustDecodeBounds 设回true,即只读边不读内容
newOptsinJustDecodeBounds = true;
newOptsinPreferredConfig = ConfigRGB_565;
// Get bitmap info, but notice that bitmap is null now
Bitmap bitmap = BitmapFactorydecodeFile(imgPath,newOpts);
newOptsinJustDecodeBounds = false;
int w = newOptsoutWidth;
int h = newOptsoutHeight;
// 想要缩放的目标尺寸
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
// 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOptsoutWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOptsoutHeight / hh);
}
if (be <= 0) be = 1;
newOptsinSampleSize = be;//设置缩放比例
// 开始压缩图片,注意此时已经把optionsinJustDecodeBounds 设回false了
bitmap = BitmapFactorydecodeFile(imgPath, newOpts);
// 压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
/**
* Compress image by size, this will modify image width/height
* Used to get thumbnail
*
* @param image
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @return
*/
public Bitmap ratio(Bitmap image, float pixelW, float pixelH) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
imagecompress(BitmapCompressFormatJPEG, 100, os);
if( ostoByteArray()length / 1024>1024) {//判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactorydecodeStream)时溢出
osreset();//重置baos即清空baos
imagecompress(BitmapCompressFormatJPEG, 50, os);//这里压缩50%,把压缩后的数据存放到baos中
}
ByteArrayInputStream is = new ByteArrayInputStream(ostoByteArray());
BitmapFactoryOptions newOpts = new BitmapFactoryOptions();
//开始读入图片,此时把optionsinJustDecodeBounds 设回true了
newOptsinJustDecodeBounds = true;
newOptsinPreferredConfig = ConfigRGB_565;
Bitmap bitmap = BitmapFactorydecodeStream(is, null, newOpts);
newOptsinJustDecodeBounds = false;
int w = newOptsoutWidth;
int h = newOptsoutHeight;
float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了
float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了
//缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可
int be = 1;//be=1表示不缩放
if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
be = (int) (newOptsoutWidth / ww);
} else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
be = (int) (newOptsoutHeight / hh);
}
if (be <= 0) be = 1;
newOptsinSampleSize = be;//设置缩放比例
//重新读入图片,注意此时已经把optionsinJustDecodeBounds 设回false了
is = new ByteArrayInputStream(ostoByteArray());
bitmap = BitmapFactorydecodeStream(is, null, newOpts);
//压缩好比例大小后再进行质量压缩
// return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除
return bitmap;
}
/**
* Compress by quality, and generate image to the path specified
*
* @param image
* @param outPath
* @param maxSize target will be compressed to be smaller than this size(kb)
* @throws IOException
*/
public void compressAndGenImage(Bitmap image, String outPath, int maxSize) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
// scale
int options = 100;
// Store the bitmap into output stream(no compress)
imagecompress(BitmapCompressFormatJPEG, options, os);
// Compress by loop
while ( ostoByteArray()length / 1024 > maxSize) {
// Clean up os
osreset();
// interval 10
options -= 10;
imagecompress(BitmapCompressFormatJPEG, options, os);
}
// Generate compressed image file
FileOutputStream fos = new FileOutputStream(outPath);
foswrite(ostoByteArray());
fosflush();
fosclose();
}
/**
* Compress by quality, and generate image to the path specified
*
* @param imgPath
* @param outPath
* @param maxSize target will be compressed to be smaller than this size(kb)
* @param needsDelete Whether delete original file after compress
* @throws IOException
*/
public void compressAndGenImage(String imgPath, String outPath, int maxSize, boolean needsDelete) throws IOException {
compressAndGenImage(getBitmap(imgPath), outPath, maxSize);
// Delete original file
if (needsDelete) {
File file = new File (imgPath);
if (fileexists()) {
filedelete();
}
}
}
/**
* Ratio and generate thumb to the path specified
*
* @param image
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @throws FileNotFoundException
*/
public void ratioAndGenThumb(Bitmap image, String outPath, float pixelW, float pixelH) throws FileNotFoundException {
Bitmap bitmap = ratio(image, pixelW, pixelH);
storeImage( bitmap, outPath);
}
/**
* Ratio and generate thumb to the path specified
*
* @param image
* @param outPath
* @param pixelW target pixel of width
* @param pixelH target pixel of height
* @param needsDelete Whether delete original file after compress
* @throws FileNotFoundException
*/
public void ratioAndGenThumb(String imgPath, String outPath, float pixelW, float pixelH, boolean needsDelete) throws FileNotFoundException {
Bitmap bitmap = ratio(imgPath, pixelW, pixelH);
storeImage( bitmap, outPath);
// Delete original file
if (needsDelete) {
File file = new File (imgPath);
if (fileexists()) {
filedelete();
}
}
}
}
public static void compressBmpToFile(Bitmap bmp,File file){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 80;//个人喜欢从80开始,
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
options -= 10;
bmp.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(baos.toByteArray());
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
private Bitmap compressBmpFromBmp(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int options = 100;
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
while (baos.toByteArray().length / 1024 > 100) {
baos.reset();
options -= 10;
image.compress(Bitmap.CompressFormat.JPEG, options, baos);
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
return bitmap;
}
private Bitmap compressImageFromFile(String srcPath) {
BitmapFactory.Options newOpts = new BitmapFactory.Options();
newOpts.inJustDecodeBounds = true;//只读边,不读内容
Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
newOpts.inJustDecodeBounds = false;
int w = newOpts.outWidth;
int h = newOpts.outHeight;
float hh = 800f;//
float ww = 480f;//
int be = 1;
if (w > h && w > ww) {
be = (int) (newOpts.outWidth / ww);
} else if (w < h && h > hh) {
be = (int) (newOpts.outHeight / hh);
}
if (be <= 0)
be = 1;
newOpts.inSampleSize = be;//设置采样率
newOpts.inPreferredConfig = Config.ARGB_8888;//该模式是默认的,可不设
newOpts.inPurgeable = true;// 同时设置才会有效
newOpts.inInputShareable = true;//。当系统内存不够时候图片自动被回收
bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
// return compressBmpFromBmp(bitmap);//原来的方法调用了这个方法企图进行二次压缩
//其实是无效的,大家尽管尝试
return bitmap;
}
public static void compressPicture(String srcPath, String desPath) {
FileOutputStream fos = null;
BitmapFactoryOptions op = new BitmapFactoryOptions();
// 开始读入图片,此时把optionsinJustDecodeBounds 设回true了
opinJustDecodeBounds = true;
Bitmap bitmap = BitmapFactorydecodeFile(srcPath, op);
opinJustDecodeBounds = false;
// 缩放图片的尺寸
float w = opoutWidth;
float h = opoutHeight;
float hh = 1024f;//
float ww = 1024f;//
// 最长宽度或高度1024
float be = 0f;
if (w > h && w > ww) {
be = (float) (w / ww);
} else if (w < h && h > hh) {
be = (float) (h / hh);
}
if (be <= 0) {
be = 0f;
}
opinSampleSize = (int) be;// 设置缩放比例,这个数字越大,图片大小越小
// 重新读入图片,注意此时已经把optionsinJustDecodeBounds 设回false了
bitmap = BitmapFactorydecodeFile(srcPath, op);
int desWidth = (int) (w / be);
int desHeight = (int) (h / be);
bitmap = BitmapcreateScaledBitmap(bitmap, desWidth, desHeight, true);
try {
fos = new FileOutputStream(desPath);
if (bitmap != null) {
bitmapcompress(BitmapCompressFormatJPEG, 100, fos);
}
} catch (FileNotFoundException e) {
eprintStackTrace();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有