可以,压缩只是一种算法,什么语言都可以,比如某种格式的文件中1001010(二进制)代表汉子的"中"字,那么压缩算法就是在编码不冲突的情况下可以改变编码长度,比如压缩之后中字变成1010,这样就节省空间了,这是我随便举的例子,具体的对应算法可以网上查
可以使用Draw这个类,通过改变像素来改变存储大小,实例如下:
public static boolean compressPic(String srcFilePath, String descFilePath) throws IOException {File file = null
BufferedImage src = null
FileOutputStream out = null
ImageWriter imgWrier
ImageWriteParam imgWriteParams
// 指定写图片的方式为 jpg
imgWrier = ImageIO.getImageWritersByFormatName("jpg").next()
imgWriteParams = new javax.imageio.plugins.jpeg.JPEGImageWriteParam(
null)
// 要使用压缩,必须指定压缩方式为MODE_EXPLICIT
imgWriteParams.setCompressionMode(imgWriteParams.MODE_EXPLICIT)
// 这里指定压缩的程度,参数qality是取值0~1范围内,
imgWriteParams.setCompressionQuality((float) 1)
imgWriteParams.setProgressiveMode(imgWriteParams.MODE_DISABLED)
ColorModel colorModel =ImageIO.read(new File(srcFilePath)).getColorModel()// ColorModel.getRGBdefault()
// 指定压缩时使用的色彩模式
// imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
// colorModel, colorModel.createCompatibleSampleModel(16, 16)))
imgWriteParams.setDestinationType(new javax.imageio.ImageTypeSpecifier(
colorModel, colorModel.createCompatibleSampleModel(16, 16)))
try {
if (isBlank(srcFilePath)) {
return false
} else {
file = new File(srcFilePath)System.out.println(file.length())
src = ImageIO.read(file)
out = new FileOutputStream(descFilePath)
imgWrier.reset()
// 必须先指定 out值,才能调用write方法, ImageOutputStream可以通过任何
// OutputStream构造
imgWrier.setOutput(ImageIO.createImageOutputStream(out))
// 调用write方法,就可以向输入流写图片
imgWrier.write(null, new IIOImage(src, null, null),
imgWriteParams)
out.flush()
out.close()
}
} catch (Exception e) {
e.printStackTrace()
return false
}
return true
}
public static boolean isBlank(String string) {
if (string == null || string.length() == 0 || string.trim().equals("")) {
return true
}
return false
}
//将图按比例缩小。public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
// targetW,targetH分别表示目标长和宽
int type = source.getType()
BufferedImage target = null
double sx = (double) targetW / source.getWidth()
double sy = (double) targetH / source.getHeight()
//这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
//则将下面的if else语句注释即可
if(sx>sy)
{
sx = sy
targetW = (int)(sx * source.getWidth())
}else{
sy = sx
targetH = (int)(sy * source.getHeight())
}
if (type == BufferedImage.TYPE_CUSTOM) { //handmade
ColorModel cm = source.getColorModel()
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH)
boolean alphaPremultiplied = cm.isAlphaPremultiplied()
target = new BufferedImage(cm, raster, alphaPremultiplied, null)
} else
target = new BufferedImage(targetW, targetH, type)
Graphics2D g = target.createGraphics()
//smoother than exlax:
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY )
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy))
g.dispose()
return target
}
public static void saveImageAsJpg (String fromFileStr,String saveToFileStr,int width,int hight)
throws Exception {
BufferedImage srcImage
// String ex = fromFileStr.substring(fromFileStr.indexOf("."),fromFileStr.length())
String imgType = "JPEG"
if (fromFileStr.toLowerCase().endsWith(".png")) {
imgType = "PNG"
}
// System.out.println(ex)
File saveFile=new File(saveToFileStr)
File fromFile=new File(fromFileStr)
srcImage = ImageIO.read(fromFile)
if(width >0 || hight >0)
{
srcImage = resize(srcImage, width, hight)
}
ImageIO.write(srcImage, imgType, saveFile)
}
public static void main (String argv[]) {
try{
//参数1(from),参数2(to),参数3(宽),参数4(高)
saveImageAsJpg("C:\\Documents and Settings\\xugang\\桌面\\tmr-06.jpg",
"C:\\Documents and Settings\\xugang\\桌面\\2.jpg",
120,120)
} catch(Exception e){
e.printStackTrace()
}
}