怎样使用java读取image
2个回答
展开全部
这是一个图片压缩代码,以前入职的时候照片必须得20k一下,所以我把100多k的照片压缩成了16k,你可以看一下
package com.atee.test;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
public class ImageShrink {
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) 0.7);
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 void main(String[] args) throws IOException {
compressPic("C:\\358x441 - 111.jpg", "C:\\358x441 - 333.jpg");
}
}
package com.atee.test;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
public class ImageShrink {
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) 0.7);
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 void main(String[] args) throws IOException {
compressPic("C:\\358x441 - 111.jpg", "C:\\358x441 - 333.jpg");
}
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
前台:
<img border="0" width="1000"height="600" src="docInfo.do?method=XXX>" />
后台:
if (StringUtils.isNotBlank(XXXX)) {
DocInfo docInfo = new DocInfo();
byte[] docFile = null;
//查询图片的sql,在这列出来
String sql = "";
docInfo = docInfoService.getFile(sql);
if (null != docInfo) {
docFile = docInfo.getDocFile();
}
if (null != docFile) {
ServletOutputStream sos;
try {
sos = response.getOutputStream();
sos.write(docFile);
sos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
<img border="0" width="1000"height="600" src="docInfo.do?method=XXX>" />
后台:
if (StringUtils.isNotBlank(XXXX)) {
DocInfo docInfo = new DocInfo();
byte[] docFile = null;
//查询图片的sql,在这列出来
String sql = "";
docInfo = docInfoService.getFile(sql);
if (null != docInfo) {
docFile = docInfo.getDocFile();
}
if (null != docFile) {
ServletOutputStream sos;
try {
sos = response.getOutputStream();
sos.write(docFile);
sos.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询