package com.baitw.struts.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
/**
*
* 生成等比例高质量缩略图
*
* */
public class ScaleImage {
private int width;
private int height;
private int scaleWidth;
double support = (double) 3.0;
double PI = (double) 3.14159265358978;
double[] contrib;
double[] normContrib;
double[] tmpContrib;
int startContrib, stopContrib;
int nDots;
int nHalfDots;
/**
* Start: Use Lanczos filter to replace the original algorithm for image
* scaling. Lanczos improves quality of the scaled image modify by :blade
**/
// fromFileStr原图片地址,saveToFileStr生成缩略图地址,formatWideth生成图片宽度,formatHeight高度
public void saveImageAsJpg(String fromFileStr, String saveToFileStr,
int formatWideth, int formatHeight) throws Exception {
BufferedImage srcImage;
File saveFile = new File(saveToFileStr);
File fromFile = new File(fromFileStr);
srcImage = javax.imageio.ImageIO.read(fromFile); // construct image
int imageWideth = srcImage.getWidth(null);
int imageHeight = srcImage.getHeight(null);
int changeToWideth = 0;
int changeToHeight = 0;
if (imageWideth > 0 && imageHeight > 0) {
// flag=true;
if (imageWideth / imageHeight >= formatWideth / formatHeight) {
if (imageWideth > formatWideth) {
changeToWideth = formatWideth;
changeToHeight = (imageHeight * formatWideth) / imageWideth;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
} else {
if (imageHeight > formatHeight) {
changeToHeight = formatHeight;
changeToWideth = (imageWideth * formatHeight) / imageHeight;
} else {
changeToWideth = imageWideth;
changeToHeight = imageHeight;
}
}
}
srcImage = imageZoomOut(srcImage, changeToWideth, changeToHeight);
ImageIO.write(srcImage, "JPEG", saveFile);
}
public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {
width = srcBufferImage.getWidth();
height = srcBufferImage.getHeight();
scaleWidth = w;
if (DetermineResultSize(w, h) == 1) {
return srcBufferImage;
}
CalContrib();
BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
return pbFinalOut;
}
//决定图像尺寸
private int DetermineResultSize(int w, int h) {
double scaleH, scaleV;
scaleH = (double) w / (double) width;
scaleV = (double) h / (double) height;
// 需要判断一下scaleH,scaleV,不做放大操作
if (scaleH >= 1.0 && scaleV >= 1.0) {
return 1;
}
return 0;
} // end of DetermineResultSize()
private double Lanczos(int i, int inWidth, int outWidth, double Support) {
double x;
x = (double) i * (double) outWidth / (double) inWidth;
return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)
/ (x * PI / Support);
}
private void CalContrib() {
nHalfDots = (int) ((double) width * support / (double) scaleWidth);
nDots = nHalfDots * 2 + 1;
try {
contrib = new double[nDots];
normContrib = new double[nDots];
tmpContrib = new double[nDots];
} catch (Exception e) {
System.out.println("init contrib,normContrib,tmpContrib" + e);
}
int center = nHalfDots;
contrib[center] = 1.0;
double weight = 0.0;
int i = 0;
for (i = 1; i <= center; i++) {
contrib[center + i] = Lanczos(i, width, scaleWidth, support);
weight += contrib[center + i];
}
for (i = center - 1; i >= 0; i--) {
contrib[i] = contrib[center * 2 - i];
}
weight = weight * 2 + 1.0;
for (i = 0; i <= center; i++) {
normContrib[i] = contrib[i] / weight;
}
for (i = center + 1; i < nDots; i++) {
normContrib[i] = normContrib[center * 2 - i];
}
} // end of CalContrib()
// 处理边缘
private void CalTempContrib(int start, int stop) {
double weight = 0;
int i = 0;
for (i = start; i <= stop; i++) {
weight += contrib[i];
}
for (i = start; i <= stop; i++) {
tmpContrib[i] = contrib[i] / weight;
}
} // end of CalTempContrib()
private int GetRedValue(int rgbValue) {
int temp = rgbValue & 0x00ff0000;
return temp >> 16;
}
private int GetGreenValue(int rgbValue) {
int temp = rgbValue & 0x0000ff00;
return temp >> 8;
}
private int GetBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}
private int ComRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
}
// 行水平滤波
private int HorizontalFilter(BufferedImage bufImg, int startX, int stopX,
int start, int stop, int y, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startX, j = start; i <= stopX; i++, j++) {
valueRGB = bufImg.getRGB(i, y);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
return valueRGB;
} // end of HorizontalFilter()
// 图片水平滤波
private BufferedImage HorizontalFiltering(BufferedImage bufImage, int iOutW) {
int dwInW = bufImage.getWidth();
int dwInH = bufImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iOutW, dwInH,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < iOutW; x++) {
int startX;
int start;
int X = (int) (((double) x) * ((double) dwInW) / ((double) iOutW) + 0.5);
int y = 0;
startX = X - nHalfDots;
if (startX < 0) {
startX = 0;
start = nHalfDots - X;
} else {
start = 0;
}
int stop;
int stopX = X + nHalfDots;
if (stopX > (dwInW - 1)) {
stopX = dwInW - 1;
stop = nHalfDots + (dwInW - 1 - X);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (y = 0; y < dwInH; y++) {
value = HorizontalFilter(bufImage, startX, stopX, start,
stop, y, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of HorizontalFiltering()
private int VerticalFilter(BufferedImage pbInImage, int startY, int stopY,
int start, int stop, int x, double[] pContrib) {
double valueRed = 0.0;
double valueGreen = 0.0;
double valueBlue = 0.0;
int valueRGB = 0;
int i, j;
for (i = startY, j = start; i <= stopY; i++, j++) {
valueRGB = pbInImage.getRGB(x, i);
valueRed += GetRedValue(valueRGB) * pContrib[j];
valueGreen += GetGreenValue(valueRGB) * pContrib[j];
valueBlue += GetBlueValue(valueRGB) * pContrib[j];
// System.out.println(valueRed+"->"+Clip((int)valueRed)+"<-");
//
// System.out.println(valueGreen+"->"+Clip((int)valueGreen)+"<-");
// System.out.println(valueBlue+"->"+Clip((int)valueBlue)+"<-"+"-->");
}
valueRGB = ComRGB(Clip((int) valueRed), Clip((int) valueGreen),
Clip((int) valueBlue));
// System.out.println(valueRGB);
return valueRGB;
} // end of VerticalFilter()
private BufferedImage VerticalFiltering(BufferedImage pbImage, int iOutH) {
int iW = pbImage.getWidth();
int iH = pbImage.getHeight();
int value = 0;
BufferedImage pbOut = new BufferedImage(iW, iOutH,
BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < iOutH; y++) {
int startY;
int start;
int Y = (int) (((double) y) * ((double) iH) / ((double) iOutH) + 0.5);
startY = Y - nHalfDots;
if (startY < 0) {
startY = 0;
start = nHalfDots - Y;
} else {
start = 0;
}
int stop;
int stopY = Y + nHalfDots;
if (stopY > (int) (iH - 1)) {
stopY = iH - 1;
stop = nHalfDots + (iH - 1 - Y);
} else {
stop = nHalfDots * 2;
}
if (start > 0 || stop < nDots - 1) {
CalTempContrib(start, stop);
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, tmpContrib);
pbOut.setRGB(x, y, value);
}
} else {
for (int x = 0; x < iW; x++) {
value = VerticalFilter(pbImage, startY, stopY, start, stop,
x, normContrib);
pbOut.setRGB(x, y, value);
}
}
}
return pbOut;
} // end of VerticalFiltering()
int Clip(int x) {
if (x < 0)
return 0;
if (x > 255)
return 255;
return x;
}
}
package com.baitw.struts.action;
import java.io.File;
import java.util.Date;
import org.apache.struts2.ServletActionContext;
import com.baitw.dao.ActivityDao;
import com.baitw.dao.CardTypeDao;
import com.baitw.dao.ImageDao;
import com.baitw.dao.UserDao;
import com.baitw.hibernate.entity.TActivity;
import com.opensymphony.xwork2.ActionSupport;
/**
*
* 添加活动
*
* */
public class AddActivity extends ActionSupport {
private String title;
private Date begintime;
private Date endtime;
private String content;
private Boolean state;
private Date createtime;
private Long cardTypeid;
private String username;
private ActivityDao activityDao;
private CardTypeDao cardTypeDao;
private UserDao userDao;
/*-----------------------------------------------*/
private String picture;
private File upload;
private String uploadContextType;
private String uploadFileName;
private String savePath;
private ImageDao imageDao;
private int width;
private int height;
public void setImageDao(ImageDao imageDao) {
this.imageDao = imageDao;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContextType() {
return uploadContextType;
}
public void setUploadContextType(String uploadContextType) {
this.uploadContextType = uploadContextType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getRequest().getRealPath(savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
/*-----------------------------------------------*/
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void setCardTypeDao(CardTypeDao cardTypeDao) {
this.cardTypeDao = cardTypeDao;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getBegintime() {
return begintime;
}
public void setBegintime(Date begintime) {
this.begintime = begintime;
}
public Date getEndtime() {
return endtime;
}
public void setEndtime(Date endtime) {
this.endtime = endtime;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Boolean getState() {
return state;
}
public void setState(Boolean state) {
this.state = state;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public Long getCardTypeid() {
return cardTypeid;
}
public void setCardTypeid(Long cardTypeid) {
this.cardTypeid = cardTypeid;
}
public ActivityDao getActivityDao() {
return activityDao;
}
public void setActivityDao(ActivityDao activityDao) {
this.activityDao = activityDao;
}
@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
TActivity activity=new TActivity();
activity.setTitle(title);
activity.setTCardType(cardTypeDao.getTCardType(cardTypeid.toString()));
activity.setCreatetime(createtime);
activity.setBegintime(begintime);
activity.setEndtime(endtime);
activity.setContent(content);
activity.setLogo(savePath+"/"+uploadFileName);
activity.setLogo(picture);
activity.setState(true);
activity.setTUser(userDao.getUserEntity(username));
imageDao.UploadOriginalImage(upload,
ServletActionContext.getRequest().getRealPath(savePath),
uploadFileName);
width=400;height=300;
String fileType = uploadFileName.substring(uploadFileName.lastIndexOf("."));
imageDao.formatImage(ServletActionContext.getRequest().getRealPath(savePath)
+"\\"+uploadFileName,
ServletActionContext.getRequest().getRealPath(savePath)
+"\\"+uploadFileName+"_"+width+"_"+height+fileType,
width,
height);
return activityDao.addActivity(activity);
}
public String loadAddPage() throws Exception{
return "loadAddPage";
}
}
分享到:
相关推荐
"图片按照指定宽度或高度等比例压缩图片以及裁剪图片"这个主题涉及到图像处理的关键技术,包括图像缩放和裁剪。接下来,我们将深入探讨这两个概念。 首先,**等比例压缩图片**是保持图片原有宽高比的情况下调整图片...
图片压缩是常见的图片处理技术之一,尤其是等比例压缩图片,在不牺牲图片质量和观感的前提下减少文件大小。本文将详细讲解PHP语言如何实现等比例压缩图片,主要侧重于以下几个方面: 1. 图片压缩概念与等比例压缩:...
等比例压缩图片的类,ASP图片压缩资源较少,这是一个相对简单的类,容易学习。
从开源代码中总结的php图片等比例缩放 测试可用
5. 等比例压缩图片:根据图片原始尺寸和设定的最大尺寸,计算出压缩后图片的宽度和高度。如果图片宽度超过最大宽度,或高度超过最大高度,则按照图片的原始长宽比来调整压缩后的尺寸。在这段代码里,如果宽度超过...
图片等比例压缩或等比例截取,可以实现图片简单操作
等比例压缩图片的核心概念是保持图片的宽高比,即图像的宽度与高度之间的相对比例关系不变。当图片尺寸被缩放时,保证其缩放后仍能够维持原有比例不变,避免图片变形。 在给出的PHP代码示例中,我们首先通过`...
图片等比例压缩,同时图片大小压缩。经本人测试,800k的图片压缩过后只有几十k,图片效果差不多。
该图片压缩类提供了多种压缩图片的方法,包括直接压缩`Bitmap`对象、将压缩后的图片保存至流或文件、以及从流或文件读取图片进行压缩。每种方法都允许开发者指定压缩级别,范围通常在0到100之间,数值越低,压缩率越...
4. **图片压缩**:等比例压缩图片涉及到图像处理,这里通常会使用Canvas元素。将Base64编码的图片数据设置为Canvas的`drawImage()`方法的源,然后通过调整Canvas的宽度和高度,保持原图宽高比进行缩放。最后,使用`...
为了实现按比例压缩图片的同时保持清晰度,我们需要采用一些特殊的技术和策略。以下将详细介绍如何在PHP中实现这一目标。 1. 图片压缩原理: 图片压缩主要分为有损压缩和无损压缩两种。有损压缩(如JPEG)会丢失...
JAVA等比例图片压缩
自己用C#写的一个 WPF工具、...可以设置压缩比例、处理方式、宽高限制啥的。 在你需要批量缩略图处理、或者批量图片压缩的需求时还是蛮起作用。 想直接使用的该工具的话 debug目录下有编译后的直接使用、 自己生成也行
在IT行业中,图片压缩和等比例放缩是图像处理领域中的常见操作,广泛应用于网站优化、存储管理和多媒体设计。下面将详细阐述这两个知识点。 一、图片压缩 图片压缩是为了减小图片文件的大小,以便更有效地存储和...
在图像处理领域,图片无损压缩和无损等比例缩放是两个重要的概念,它们对于保持图像质量和节省存储空间都至关重要。以下是对这两个主题的详细解释: **图片无损压缩**: 无损压缩是一种图像压缩方式,它允许在压缩...
等比例压缩是指保持图片的宽高比不变,只根据指定的最大尺寸来调整图片的长边。在UIImage+ImageScaleSize.m中,我们可以找到这样的方法: ```objc - (UIImage *)imageByScalingProportionallyToSize:(CGSize)target...
"上传图片时按比例压缩"是一个重要的技术环节,旨在减少图片文件大小,提高网站加载速度,节省存储空间,同时保持图片质量。本文将详细介绍如何实现这一功能,并介绍使用的工具——ImageMagick。 ImageMagick是一款...
本文将详细探讨"图片处理,剪切,非比例压缩,等比压缩"这些知识点,并结合灰度处理进行深入解析。 首先,图片处理是指对图像进行的一系列操作,以达到美化、编辑或优化的效果。这可能包括调整亮度和对比度,去除...