㈠ java上传图片 生成缩略图,如果上传的图片尺寸比较小就压缩处理
//将图按比例缩小。
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.(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();
}
}
㈡ 如何利用JAVA直接生成Flash(SWF格式)文件的缩略图谢谢了,大神帮忙啊
不知用Java怎么直接生成,只知道用fla格式转换成swf格式,就用Flash8保存为fla格式,再在预览时按Ctrl+Enter键,就可以生成一个SWF格式的文件,希望有用
㈢ javaWeb怎么实现根据内容生成缩略图
packagecom.hoo.util;
importjava.awt.Image;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.net.MalformedURLException;
importjava.net.URL;
importjavax.imageio.ImageIO;
importcom.sun.image.codec.jpeg.ImageFormatException;
importcom.sun.image.codec.jpeg.JPEGCodec;
importcom.sun.image.codec.jpeg.JPEGEncodeParam;
importcom.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*<b>function:</b>缩放图片工具类,创建缩略图、伸缩图片比例
*@authorhoojo
*@createDate2012-2-3上午10:08:47
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@version1.0
*/
{
_SCALE_QUALITY=1f;
_IMAGE_FORMAT=".jpg";//图像文件的格式
_FILE_PATH="C:/temp-";
/**
*<b>function:</b>设置图片压缩质量枚举类;
*Someguidelines:0.75highquality、0.5mediumquality、0.25lowquality
*@authorhoojo
*@createDate2012-2-7上午11:31:45
*@fileScaleImageUtils.java
*@packagecom.hoo.util
*@projectJQueryMobile
*@version1.0
*/
publicenumImageQuality{
max(1.0f),high(0.75f),medium(0.5f),low(0.25f);
privateFloatquality;
publicFloatgetQuality(){
returnthis.quality;
}
ImageQuality(Floatquality){
this.quality=quality;
}
}
privatestaticImageimage;
/**
*<b>function:</b>通过目标对象的大小和标准(指定)大小计算出图片缩小的比例
*@authorhoojo
*@createDate2012-2-6下午04:41:48
*@paramtargetWidth目标的宽度
*@paramtargetHeight目标的高度
*@paramstandardWidth标准(指定)宽度
*@paramstandardHeight标准(指定)高度
*@return最小的合适比例
*/
publicstaticdoublegetScaling(doubletargetWidth,doubletargetHeight,doublestandardWidth,doublestandardHeight){
doublewidthScaling=0d;
doubleheightScaling=0d;
if(targetWidth>standardWidth){
widthScaling=standardWidth/(targetWidth*1.00d);
}else{
widthScaling=1d;
}
if(targetHeight>standardHeight){
heightScaling=standardHeight/(targetHeight*1.00d);
}else{
heightScaling=1d;
}
returnMath.min(widthScaling,heightScaling);
}
㈣ java后台得到图片流,直接通过图片流得到60*60缩略图的流且图片比例不变。
这个不好弄吧,如果是个100*10的图片上传上来,你也压缩到60*60,比例肯定变了啊,不然就按比例缩小也可以,但是不能保证是60*60了,我有处理图片大小的代码,只能是按比例缩小,或者是指定宽,按原图比例缩小图片,直接指定宽高也可以。
㈤ 如何利用Java生成JPG缩略图
public static boolean scale(String imagepath,String newpath){
// 返回一个 BufferedImage,作为使用从当前已注册 ImageReader 中自动选择的 ImageReader 解码所提供 File 的结果
BufferedImage image=null;
try {
image = ImageIO.read(new File(imagepath));
} catch (IOException e) {
System.out.println("读取图片文件出错!"+e.getMessage());
return false;
}
// Image Itemp = image.getScaledInstance(300, 300, image.SCALE_SMOOTH);
double Ratio = 0.0;
if ((image.getHeight() > 300) ||(image.getWidth() > 300)) {
if (image.getHeight() > image.getWidth())
//图片要缩放的比例
Ratio = 300.0 / image.getHeight();
else
Ratio = 300.0 / image.getWidth();
}
// 根据仿射转换和插值类型构造一个 AffineTransformOp。
AffineTransformOp op = new AffineTransformOp(AffineTransform
.getScaleInstance(Ratio, Ratio), null);
// 转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
image = op.filter(image,null);
//image.getScaledInstance(300,300,image.SCALE_SMOOTH);
FileOutputStream out=null;
try {
out = new FileOutputStream(newpath);
ImageIO.write((BufferedImage)image,"bmp",out);
out.close();
} catch (Exception e) {
System.out.println("写图片文件出错!!"+e.getMessage());
return false;
}
return true;
}
㈥ 有人知道java生成缩略图的方法吗 thumbnailator有问题
自己写了一个,看看能不能有
/**
*本类实现一个对JPG/JPEG图像文件进行缩放处理的方法:即给定一个JPG文件,可以生成一个该JPG文件的缩影图像文件(JPG格式)<br/>
*提供三种生成缩影图像的方法:<br/>
*1.设置缩影文件的宽度,根据设置的宽度和源图像文件的大小来确定新缩影文件的长度来生成缩影图像<br/>
*2.设置缩影文件的长度,根据设置的长度和源图像文件的大小来确定新缩影文件的宽度来生成缩影图像<br/>
*3.设置缩影文件相对于源图像文件的比例大小,根据源图像文件的大小及设置的比例来确定新缩影文件的大小来生成缩影图像<br/>
*新生成的缩影图像可以比原图像大,这时即是放大源图像<br/>
*
*@author不落的太阳(SeanYang)
*@version1.0
*@sinceJDK1.8
*
*/
publicclassImageScalingTool{
//对象是否己经初始化
privatebooleanisInitFlag=false;
//定义生目标图片的宽度和高度,给其一个就可以了
privateinttargetPicWidth=0;
privateinttargetPicHeight=0;
//定义目标图片的相比原图片的比例
privatedoublepicScale=0;
/**
*构造函数
*/
publicImageScalingTool(){
isInitFlag=false;
}
/**
*重置JPG图片缩放器
*/
publicvoidresetImageScaling(){
picScale=0;
targetPicWidth=0;
targetPicHeight=0;
isInitFlag=false;
}
/**
*设置目标图片相对于源图片的缩放比例
*
*@paramscale
*@throwsJPEGException
*/
publicvoidsetPicScale(doublescale)throwsException{
if(scale<=0){
thrownewRuntimeException("缩放比例不能为0和负数!");
}
resetImageScaling();
picScale=scale;
isInitFlag=true;
}
/**
*设置目标图片的宽度
*
*@paramwidth
*@throwsJPEGException
*/
publicvoidsetSmallWidth(intwidth)throwsException{
if(width<=0){
thrownewRuntimeException("缩影图片的宽度不能为0和负数!");
}
resetImageScaling();
targetPicWidth=width;
isInitFlag=true;
}
/**
*设置目标图片的高度
*
*@paramheight
*@throwsJPEGException
*/
publicvoidsetSmallHeight(intheight)throwsException{
if(height<=0){
thrownewRuntimeException("缩影图片的高度不能为0和负数!");
}
resetImageScaling();
targetPicHeight=height;
isInitFlag=true;
}
/**
*开始缩放图片
*
*@paramsrcPicFileName
*源图片的文件名
*@paramtargetPicFileName
*生成目标图片的文件名
*@throwsJPEGException
*/
publicvoidtransform(StringsrcPicFileName,StringtargetPicFileName)throwsException{
if(!isInitFlag){
thrownewRuntimeException("对象参数没有初始化!");
}
if(srcPicFileName==null||targetPicFileName==null){
thrownewRuntimeException("包含文件名的路径为空!");
}
if((!srcPicFileName.toLowerCase().endsWith("jpg"))&&(!srcPicFileName.toLowerCase().endsWith("jpeg"))){
thrownewRuntimeException("只能处理JPG/JPEG文件!");
}
if((!targetPicFileName.toLowerCase().endsWith("jpg"))&&!targetPicFileName.toLowerCase().endsWith("jpeg")){
thrownewRuntimeException("只能处理JPG/JPEG文件!");
}
//新建源图片和生成图片的文件对象
Filefin=newFile(srcPicFileName);
Filefout=newFile(targetPicFileName);
//通过缓冲读入源图片文件
BufferedImagesourceImage=null;
try{
//读取文件生成BufferedImage
sourceImage=ImageIO.read(fin);
}catch(IOExceptionex){
thrownewRuntimeException("读取源图像文件出错!");
}
//源图片的宽度和高度
intsourceWidth=sourceImage.getWidth();
intsourceHeight=sourceImage.getHeight();
//设置目标图片的实际宽度和高度
inttargetWidth=0;
inttargetHeight=0;
if(targetPicWidth!=0){
//根据设定的宽度求出长度
targetWidth=targetPicWidth;
targetHeight=(targetWidth*sourceHeight)/sourceWidth;
}elseif(targetPicHeight!=0){
//根据设定的长度求出宽度
targetHeight=targetPicHeight;
targetWidth=(targetHeight*sourceWidth)/sourceHeight;
}elseif(picScale!=0){
//根据设置的缩放比例设置图像的长和宽
targetWidth=(int)(sourceWidth*picScale);
targetHeight=(int)(sourceHeight*picScale);
}else{
thrownewRuntimeException("对象参数初始化不正确!");
}
System.out.println("源图片的分辨率:"+sourceWidth+"×"+sourceHeight);
System.out.println("目标图片的分辨率:"+targetWidth+"×"+targetHeight);
//目标图像的缓冲对象
BufferedImagetargetImage=newBufferedImage(targetWidth,targetHeight,BufferedImage.TYPE_3BYTE_BGR);
//求得目标图片与源图片宽度,高度的比例.
doublescaleWidth=(double)targetWidth/sourceWidth;
doublescaleHeight=(double)targetHeight/sourceHeight;
//构造图像变换对象
AffineTransformtransform=newAffineTransform();
//设置图像转换的比例
transform.setToScale(scaleWidth,scaleHeight);
//构造图像转换操作对象
AffineTransformOpato=newAffineTransformOp(transform,null);
//实现转换,将bSrc转换成bTarget
ato.filter(sourceImage,targetImage);
//输出目标图片
try{
//将目标图片的BufferedImage写到文件中去,jpeg为图片的格式
ImageIO.write(targetImage,"jpeg",fout);
}catch(IOExceptionex1){
thrownewException("写入缩略图像文件出错!");
}
}
}
㈦ JAVA开发微信公众号图文素材缩略图,怎么设置图文缩略图
你好。 如果要把别人的图片水印变成自己的,那么首先就是用PS软件P除掉该图片上的水印,然后将P过的图片加入微信公众平台素材库里。 方法一:点击微信公众平台后台的公众号设置——功能设置里面有个图片水印 然后你就可以选着修改水印或者图片不添...
㈧ java实现图片预览功能,可以显示缩列图,具有上下页的功能求技术支持
把图片按照规定的比例压缩,然后保存至FTP,列表读取缩略图,单击显示原图。
/**
*压缩图片方法一(高质量)
*@paramoldFile将要压缩的图片
*@paramwidth压缩宽
*@paramheight压缩高
*@paramsmallIcon压缩图片后,添加的扩展名(在图片后缀名前添加)
*@paramquality压缩质量范围:<i>0.0-1.0</i>高质量:<i>0.75</i>中等质量:<i>0.5</i>低质量:<i>0.25</i>
*@parampercentage是否等比压缩若true宽高比率将将自动调整
*/
publicstaticvoidcompressImage(StringoldFile,intwidth,intheight,StringsmallIcon,
floatquality,booleanpercentage){
try{
Filefile=newFile(oldFile);
//验证文件是否存在
if(!file.exists())
thrownewFileNotFoundException("找不到原图片!");
//获取图片信息
BufferedImageimage=ImageIO.read(file);
intorginalWidth=image.getWidth();
intorginalHeight=image.getHeight();
//验证压缩图片信息
if(width<=0||height<=0||!Pattern.matches("^[1-9]\d*$",String.valueOf(width))
||!Pattern.matches("^[1-9]\d*$",String.valueOf(height)))
thrownewException("图片压缩后的高宽有误!");
//等比压缩
if(percentage){
doublerate1=((double)orginalWidth)/(double)width+0.1;
doublerate2=((double)orginalHeight)/(double)height+0.1;
doublerate=rate1>rate2?rate1:rate2;
width=(int)(((double)orginalWidth)/rate);
height=(int)(((double)orginalHeight)/rate);
}
//压缩后的文件名
StringfilePrex=oldFile.substring(0,oldFile.lastIndexOf('.'));
StringnewImage=filePrex+smallIcon+oldFile.substring(filePrex.length());
//压缩文件存放位置
FilesavedFile=newFile(newImage);
//创建一个新的文件
savedFile.createNewFile();
//创建原图像的缩放版本
Imageimage2=image.getScaledInstance(width,height,Image.SCALE_AREA_AVERAGING);
//创建数据缓冲区图像
BufferedImagebufImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//创建一个Graphics2D
Graphics2Dg2=bufImage.createGraphics();
//重绘图像
g2.drawImage(image2,0,0,width,height,null);
g2.dispose();
//过滤像素矩阵
float[]kernelData={
-0.125f,-0.125f,-0.125f,
-0.125f,2,-0.125f,-0.125f,
-0.125f,-0.125f};
Kernelkernel=newKernel(3,3,kernelData);
//按核数学源图像边缘的像素复制为目标中相应的像素输出像素
ConvolveOpcOp=newConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
//转换像素
bufImage=cOp.filter(bufImage,null);
FileOutputStreamout=newFileOutputStream(savedFile);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(bufImage);
//设置压缩质量
param.setQuality(quality,true);
encoder.encode(bufImage,param);
out.close();
System.out.println(newImage);
}catch(Exceptione){
e.printStackTrace();
System.out.println("压缩失败!"+e.getMessage());
}
}
㈨ 图片怎么样生成缩略图啊 java
您可以用jquery组件有现成的 会直接生成缩略图
㈩ java 图片缩放代码
直接给你一个类,直接套用就好了
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.File;
import javax.imageio.ImageIO;
public class Resize {
BufferedImage bufImage;
int width;
int height;
public Resize() {
// TODO Auto-generated constructor stub
}
public Resize(String srcPath,int width,int height) {
this.width = width;
this.height = height;
try{
this.bufImage = ImageIO.read(new File(srcPath));
}catch(Exception e){
e.printStackTrace();
}
}
public static BufferedImage rize(BufferedImage srcBufImage,int width,int height){
BufferedImage bufTarget = null;
double sx = (double) width / srcBufImage.getWidth();
double sy = (double) height / srcBufImage.getHeight();
int type = srcBufImage.getType();
if(type == BufferedImage.TYPE_CUSTOM){
ColorModel cm = srcBufImage.getColorModel();
WritableRaster raster = cm.(width,
height);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
bufTarget = new BufferedImage(cm, raster, alphaPremultiplied, null);
}else
bufTarget = new BufferedImage(width, height, type);
Graphics2D g = bufTarget.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(srcBufImage, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return bufTarget;
}
}