可以在<img 里指定大小 也可以用程序实现import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;public class dfgdfg {
//图像放大或缩小的比例
private static int number = 1;
//定义目标文件的大小;如果要放大,则desth和destw为imh与multiple相乘;否则相除;
private static int desth;
private static int destw; //检查参数信息是否符合要求
private static boolean checkarg(String[] args)
{
//取得源文件信息并创建输入流
if (args.length!=3)
{
System.out.println("Usage: ResizeImage filename s/l number ");
System.out.println(" filename is the file you want resize;");
System.out.println(" s/l: s is if you resize the image to a small image; otherwise is l");
System.out.println(" number is the multiple you want to resize");
System.out.println(" For Example: ResizeImage myimage.jpg s 3 ");
return false;
}
//检查是否为jpg文件
String result = args[0].substring(args[0].lastIndexOf(".")+1);
if(!result.matches("jpg"))
{
System.out.println("Only resize JPEG files! File extention must be: jpg");
}
//检查是否指定放大或缩小的参数:s or l
if(!args[1].matches("s")&&!args[1].matches("l"))
{
System.out.println("Please specfic if you want the image smaller or larger");
}
try
{
number = Integer.parseInt(args[2]);
}
catch(NumberFormatException ne)
{
System.out.println("The multiple must be a integer.");
}
if(Integer.parseInt(args[2])>8)
{
System.out.println("File cannot larger than 8 times!");
}
return true;
}
private static void resize(String[] args)
{
try
{
//创建输入流
String imageFile = args[0];
InputStream imageIn = new FileInputStream(new File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage im = decoder.decodeAsBufferedImage();
//取得源文件大小信息;
int imh = im.getHeight(null);
int imw = im.getWidth(null); if(args[1].matches("l"))
{
desth = imh*number;
destw = imw*number;
}
else if(args[1].matches("s"))
{
desth = imh/number;
destw = imw/number;
} BufferedImage imout = new BufferedImage(destw, desth, 1);
Graphics g = imout.getGraphics();
boolean b = g.drawImage(im, 0, 0, destw, desth, null);
//输出转换后的文件信息;
FileOutputStream output = new FileOutputStream("e:/output.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(imout); imageIn.close();
output.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
} public static void main(String[] args) {
//if(!checkarg(args))
// return;
String aa[] = {"e:/24.jpg","l","10"};
resize(aa);
System.out.println("Translation Success!");
}
}
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;public class dfgdfg {
//图像放大或缩小的比例
private static int number = 1;
//定义目标文件的大小;如果要放大,则desth和destw为imh与multiple相乘;否则相除;
private static int desth;
private static int destw; //检查参数信息是否符合要求
private static boolean checkarg(String[] args)
{
//取得源文件信息并创建输入流
if (args.length!=3)
{
System.out.println("Usage: ResizeImage filename s/l number ");
System.out.println(" filename is the file you want resize;");
System.out.println(" s/l: s is if you resize the image to a small image; otherwise is l");
System.out.println(" number is the multiple you want to resize");
System.out.println(" For Example: ResizeImage myimage.jpg s 3 ");
return false;
}
//检查是否为jpg文件
String result = args[0].substring(args[0].lastIndexOf(".")+1);
if(!result.matches("jpg"))
{
System.out.println("Only resize JPEG files! File extention must be: jpg");
}
//检查是否指定放大或缩小的参数:s or l
if(!args[1].matches("s")&&!args[1].matches("l"))
{
System.out.println("Please specfic if you want the image smaller or larger");
}
try
{
number = Integer.parseInt(args[2]);
}
catch(NumberFormatException ne)
{
System.out.println("The multiple must be a integer.");
}
if(Integer.parseInt(args[2])>8)
{
System.out.println("File cannot larger than 8 times!");
}
return true;
}
private static void resize(String[] args)
{
try
{
//创建输入流
String imageFile = args[0];
InputStream imageIn = new FileInputStream(new File(imageFile));
JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(imageIn);
BufferedImage im = decoder.decodeAsBufferedImage();
//取得源文件大小信息;
int imh = im.getHeight(null);
int imw = im.getWidth(null); if(args[1].matches("l"))
{
desth = imh*number;
destw = imw*number;
}
else if(args[1].matches("s"))
{
desth = imh/number;
destw = imw/number;
} BufferedImage imout = new BufferedImage(destw, desth, 1);
Graphics g = imout.getGraphics();
boolean b = g.drawImage(im, 0, 0, destw, desth, null);
//输出转换后的文件信息;
FileOutputStream output = new FileOutputStream("e:/output.jpg");
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(imout); imageIn.close();
output.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
} public static void main(String[] args) {
//if(!checkarg(args))
// return;
String aa[] = {"e:/24.jpg","l","10"};
resize(aa);
System.out.println("Translation Success!");
}
}
解决方案 »
- org.springframework.beans.factory.BeanCreationException: 急啊
- jsf上传图片问题
- 菜鸟俺问个跟网络编程语言有关的笨问题,劳烦各位不吝指教!!
- Tomcat服务启动一段时间后出现如下的连接异常
- DWR框架初学者问题
- 如何判断一个字符串的内容是什么类型的内容?
- 请问 我想用JSP连ACCESS 请问连接代码怎么写 需要驱动吗?
- access可以远程访问么?
- 保存网页(jsp)至服务器端的巨难问题!
- 请教怎么能够在javamail中得到这封邮件是否读过?在线等!
- jsp页面之间数据互递的为题怎么样解决?
- jsp中如何让程序强行执行完毕再输出到客户端
<a href=javascript:show_picture("3276")>
<img src=/images/3276.jpg border=1 width=80>
</a>
</td>
1, 如何用HTML做像册
2, 如何在JSP的HTML的代码中调用java代码.第一部分假设楼主会(不会得另外找文档学,简单但是不方便描述)第二部分举例:
--------
<img src=/images/3276.jpg border=1 width=80>
如果有java变量的值: int m=80,那么:<img src=/images/3276.jpg border=1 width= <%=m%> >
----------
注:不需要分号