java生成高质量缩略图的代码实现
发布时间:2020-05-24 16:01:41 所属栏目:Java 来源:互联网
导读:java生成高质量缩略图的代码实现
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.MemoryImageSource;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/** *//**
* @将文件转化为缩略图
*
*/
public class jpgThumbnail {
private static final String BMP = "bmp";
private static final String PNG = "png";
private static final String GIF = "gif";
private static final String JPEG = "jpeg";
private static final String JPG = "jpg";
public static void getThumbnail(
String source,String output,int width,int height) {
boolean adjustSize=true;
if (source == null || source.equals("") || width < 1 || height < 1) {
return;
}
if(output==null || output.equals(""))
{
return;
}
source = source.toLowerCase();
if (source.endsWith(BMP)) {
BMPThumbnailHandler(source,output,width,height,adjustSize);
} else if (source.endsWith(PNG) || source.endsWith(GIF)
|| source.endsWith(JPEG) || source.endsWith(JPG)) {
thumbnailHandler(source,adjustSize);
}
}
private static void thumbnailHandler(
String source,int height,boolean adjustSize) {
try {
File sourceFile = new File(source);
if (sourceFile.exists()) {
Image image = ImageIO.read(sourceFile);
int theImgWidth = image.getWidth(null);
int theImgHeight = image.getHeight(null);
int[] size = { theImgWidth,theImgHeight };
if (adjustSize) {
size = adjustImageSize(theImgWidth,theImgHeight,height);
}
StringBuffer thumbnailFile=new StringBuffer();
//thumbnailFile.append(sourceFile.getParent());
//thumbnailFile.append(File.separatorChar);
thumbnailFile.append(output);
writeFile(image,size[0],size[1],thumbnailFile.toString());
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private static void BMPThumbnailHandler(
String source,boolean adjustSize) {
try {
File sourceFile = new File(source);
if (sourceFile.exists()) {
Image image = getBMPImage(source);
int theImgWidth = image.getWidth(null);
int theImgHeight = image.getHeight(null);
int[] size = { theImgWidth,thumbnailFile.toString());
}
} catch (Exception e) {
e.printStackTrace();
return;
}
}
private static Image getBMPImage(String source) throws Exception {
FileInputStream fs = null;
Image image = null;
try {
fs = new FileInputStream(source);
int bfLen = 14;
byte bf[] = new byte[bfLen];
fs.read(bf,bfLen); // 读取14字节BMP文件头
int biLen = 40;
byte bi[] = new byte[biLen];
fs.read(bi,biLen); // 读取40字节BMP信息头
// 源图宽度
int nWidth = (((int) bi[7] & 0xff) << 24)
| (((int) bi[6] & 0xff) << 16)
| (((int) bi[5] & 0xff) << 8) | (int) bi[4] & 0xff;
// 源图高度
int nHeight = (((int) bi[11] & 0xff) << 24)
| (((int) bi[10] & 0xff) << 16)
| (((int) bi[9] & 0xff) << 8) | (int) bi[8] & 0xff;
// 位数
int nBitCount = (((int) bi[15] & 0xff) << 8) | (int) bi[14] & 0xff;
// 源图大小
int nSizeImage = (((int) bi[23] & 0xff) << 24)
| (((int) bi[22] & 0xff) << 16)
| (((int) bi[21] & 0xff) << 8) | (int) bi[20] & 0xff;
// 对24位BMP进行解析
if (nBitCount == 24) {
int nPad = (nSizeImage / nHeight) - nWidth * 3;
int nData[] = new int[nHeight * nWidth];
byte bRGB[] = new byte[(nWidth + nPad) * 3 * nHeight];
fs.read(bRGB,(nWidth + nPad) * 3 * nHeight);
int nIndex = 0;
for (int j = 0; j < nHeight; j++) {
for (int i = 0; i < nWidth; i++) {
nData[nWidth * (nHeight - j - 1) + i] = (255 & 0xff) << 24
| (((int) bRGB[nIndex + 2] & 0xff) << 16)
| (((int) bRGB[nIndex + 1] & 0xff) << 8)
| (int) bRGB[nIndex] & 0xff;
nIndex += 3;
}
nIndex += nPad;
}
Toolkit kit = Toolkit.getDefaultToolkit();
image = kit.createImage(new MemoryImageSource(nWidth,nHeight,nData,nWidth));
}
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (fs != null) {
fs.close();
}
}
return image;
}
private static void writeFile(
Image image,String thumbnailFile) throws Exception {
if (image == null) return;
BufferedImage tag = new BufferedImage(width,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(image,null);
FileOutputStream out = null;
try {
out = new FileOutputStream(thumbnailFile);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag);
} catch (Exception e) {
e.printStackTrace();
throw new Exception(e);
} finally {
if (out != null) {
out.close();
}
}
}
private static int[] adjustImageSize(int theImgWidth,int theImgHeight,int defWidth,int defHeight) {
int[] size = { 0,0 };
float theImgHeightFloat=Float.parseFloat(String.valueOf(theImgHeight));
float theImgWidthFloat=Float.parseFloat(String.valueOf(theImgWidth));
if (theImgWidth<theImgHeight){
float scale=theImgHeightFloat/theImgWidthFloat;
size[0]=Math.round(defHeight/scale);
size[1]=defHeight;
}else{
float scale=theImgWidthFloat/theImgHeightFloat;
size[0]=defWidth;
size[1]=Math.round(defWidth/scale);
}
return size;
}
public static void main(String[] agrs) {
//zoomPicture("E:1.jpg","E:test.jpg",60,80);
getThumbnail("E:1.jpg",80);
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – 为什么不可能使用具有多态返回类型的原语类型?
- Java微信公众平台开发(5) 文本及图文消息回复的实现
- springmvc和js前端的数据传递和接收方式(两种)
- 利用SpringMVC上传文件
- 通过Java标准输出更快的输出?
- 我如何说服GroovyShell维护eval()调用的状态?
- 在Tomcat 7(JBoss EWS 2.0)盒式磁带中Openshift更新java 7到
- Java是否提供了一个允许worker在同一个线程上执行的Executo
- java – 使用反射和ClassLoader创建类的实例时的ClassCastE
- java – Web APplication的容器管理安全性
