图片压缩

/**
* 图片压缩。
* @param formatWideth 图片宽度
* @param formatHeight 图片高度
* @param fpath 临时存放路径
* @param filename 源图片名
* @param out 压缩后的图片名
* @param texts 图片的后缀名
* @return FileInputStream 对象
* @throws PhotoManageException PhotoManageException
*/
public static byte[] jMagicCompress(float formatWideth, float formatHeight, String fpath, String filename,
String out, String texts)
throws PhotoManageException
{
RunLogger.info("FileInputStream start");
byte[] bytes = null;

//源图片的完整路径
String imgpath = fpath + filename;
File file = new File(imgpath);
BufferedImage bis = null;

try
{

bis = ImageIO.read(file);

if (null == bis)
{
throw new PhotoManageException();
}
bis.flush();
int imageWideth = bis.getWidth();
int imageHeight = bis.getHeight();
if (imageWideth > formatWideth || imageHeight > formatHeight)
{
bytes = resize(formatWideth, formatHeight, imageWideth, imageHeight, imgpath);
}
else
{
bytes = getByteFromFile(file);
}
// bis.getAlphaRaster().getDataBuffer().
}
catch (IOException e1)
{
RunLogger.error("BuffredImage read failed", e1);

}

return bytes;
}
/**
* 按比例压缩图片
* @param formatWideth 压缩后的图片宽度
* @param formatHeight 压缩后的图片高度
* @param imageWideth 图片的真实宽度
* @param imageHeight 图片的真实高度
* @param oldImgPath
* @return byte[] 返回图片的字节数组
*/
private static byte[] resize(float formatWideth, float formatHeight, int imageWideth, int imageHeight,
String oldImgPath)
{
byte[] bytes = null;
// 宽度比例
float prw = imageWideth / formatWideth;

// 高度比例
float prh = imageHeight / formatHeight;

// 压缩比例
float pr = prh;
if (formatHeight == PortalConstants.SMALLFORMATWIDTH)
{
if (prw < prh)
{
pr = prw; //按比例小的压缩

}
}
else
{
if (prw > prh)
{
pr = prw; //按比例大的压缩

}
}
if (pr < 1)
{
pr = 1;
}
String s1 = String.valueOf(imageWideth / pr);
String s2 = String.valueOf(imageHeight / pr);
int nw = Integer.parseInt(s1.substring(0, s1.indexOf(".")));
int nh = Integer.parseInt(s2.substring(0, s2.indexOf(".")));
if (nw < 1 || nh < 1)
{
nw = 1;
nh = 1;
}

try
{
bytes = PhotoManageBean.getInstance().resize(oldImgPath, nw, nh);
}
catch (Exception e)
{
RunLogger.error("RESIZE METHOD EXCEPTION", e);

}
return bytes;
}

/**
* 图片管理。
* @author alvin
* @version [版本号, May 8, 2009]
* @see [相关类/方法]
* @since DPFV100R001C01B010
*/
public final class PhotoManageBean
{

private static PhotoManageBean instance = new PhotoManageBean();

private final int half = 2;

private PhotoManageBean()
{
}

public static PhotoManageBean getInstance()
{
return instance;
}

/**
* 图片压缩
* @param fromFileStr 源文件路径
* @param changeToWideth 压缩后的文件宽度
* @param changeToHeight 压缩后的文件高度
* @return boolean 值
* @see [类、类#方法、类#成员]
*/
public byte[] resize(String fromFileStr, int changeToWideth, int changeToHeight)
{
RunLogger.info("resize start");
MagickImage image = null;
//ByteArrayOutputStream os = null;
byte[] outblob = null;
try
{
System.setProperty("jmagick.systemclassloader", "no");
ImageInfo info;
DebugLogger.inf("Image file:" + fromFileStr, null);
info = new ImageInfo(fromFileStr);

image = new MagickImage(info);

MagickImage scaled = image.scaleImage(changeToWideth, changeToHeight);//小图片文件的大小.
// scaled.setFileName(saveToFileStr);
// scaled.writeImage(info);

outblob = scaled.imageToBlob(new ImageInfo());
// DebugLogger.inf("===================== " + outblob);
// byte[] outblob = scaled.imageToBlob(info);
DebugLogger.inf("Image size after compress:width=" + changeToWideth + ",height=" + changeToHeight, null);
// os = new ByteArrayOutputStream();
// os.write(outblob);
// os.flush();
//
}
catch (MagickException e)
{
RunLogger.error("File resize MagickException", e);

}
catch (Exception e)
{
RunLogger.error("File resize abnormity", e);

}
finally
{
if (null != image)
{
image.destroyImages();
image = null;
}
}

return outblob;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值