如何修改上传图片的大小
RT,主要是修改图片的长和宽。谢谢
------解决方案--------------------按原来的象素比修改长度和和宽度
------解决方案--------------------缩略图算法,网上有
------解决方案--------------------图片的长和宽和像素有关,要去下载专门的多媒体转换软件
------解决方案--------------------自己查一下资料,实现代码比较简单的
------解决方案--------------------给你一些代码啊
看看就明白了
package qx;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import com.sun.image.codec.jpeg.*;
import javax.imageio.ImageIO;
public class JpgTest {
public static void jpg_logo(String jpgName, String logoText) throws Exception {
File _file = new File(jpgName+ ".jpg "); //读入文件
Image src = ImageIO.read(_file); //构造Image对象
int w0 = src.getWidth(null); //得到源图宽
int h0 = src.getHeight(null); //得到源图长
int w2 = 800; //=w0/2
int h2 = 600; //=h0/2
int fontSize = 32;
//缩小一 半为(800,600)
BufferedImage tag = null;
tag = new BufferedImage(w2, h2, BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0,w2, h2, null); //绘制缩小后的图
//标注水印
Graphics g = tag.getGraphics();
g.setColor(Color.RED); //以下设置前景色BLACK
// g.setXORMode(Color.RED);
g.setFont(new Font( "MyFont ", Font.BOLD, fontSize)); //PLAIN,BOLD,ITALIC
// g.drawString(logoText, 10, 10+fontSize);
g.drawString(logoText, w2-fontSize*(logoText.length()+3)/2, h2-10);
g.dispose();
//保存文件,输出到文件流
FileOutputStream out = new FileOutputStream(jpgName+ "_800.jpg ");
try {
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG编码
//System.out.print(width+ "* "+h0);
}finally{
out.close();
}
}
public static void jpg_yasuo(String jpgName, String newname, int size) throws Exception {
try{
String f0 = jpgName;
File f = new File(f0); //读入文件
Image src = ImageIO.read(f); //构造Image对象
int w0 = src.getWidth(null); //得到源图宽
int h0 = src.getHeight(null); //得到源图长
String f2 = newname;
int w2 = w0 / size;
int h2 = h0 / size;
BufferedImage tag = new BufferedImage(w2, h2,BufferedImage.TYPE_INT_RGB);
tag.getGraphics().drawImage(src, 0, 0, w2, h2, null); //绘制缩小后的图
System.out.println(f0+ "( "+w0+ "* "+h0+ ") \t=> "+f2+ "( "+w2+ "* "+h2+ ") ");
//保存文件
FileOutputStream out = new FileOutputStream(f2); //输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(tag); //近JPEG编码
out.close();
}
catch(Exception e)
{
System.out.println( "进行压缩图片时 出错----- ");
e.printStackTrace();
}
}
public static void jpg_logo() throws Exception {