求java上传图片时,如何改变图片内存大小?不是改变长和宽,例如:图片3M改成90KB? 最好有源码,谢谢!

求java上传图片时,如何改变图片内存大小?不是改变长和宽,例如:图片3M改成90KB? 最好有源码,谢谢!,第1张

import javaio;
import javautilzip;
public class GZIPcompress {
public static void main(String[] args) {
try {
BufferedReader in =
new BufferedReader(
new FileReader(args[0])); //文件地址
BufferedOutputStream out =
new BufferedOutputStream(
new GZIPOutputStream(
new FileOutputStream("testgz"))); //压缩后的文件名
Systemoutprintln("Writing file");
int c;
while((c = inread()) != -1)
outwrite(c);
inclose();
outclose();
Systemoutprintln("Reading file");
BufferedReader in2 =
new BufferedReader(
new InputStreamReader(
new GZIPInputStream(
new FileInputStream("testgz"))));
String s;
while((s = in2readLine()) != null)
Systemoutprintln(s);
} catch(Exception e) {
eprintStackTrace();
}
}
}
试试这个。。。。。。。。。。。。

可以通过以下工具方法来实现的大小设置。
public void setIcon(String file, JButton iconButton) {
ImageIcon icon = new ImageIcon(file);
Image temp = icongetImage()getScaledInstance(iconButtongetWidth(),
iconButtongetHeight(), icongetImage()SCALE_DEFAULT);
icon = new ImageIcon(temp);
iconButtonsetIcon(icon);
}
备注:file是的路径,iconButton是按钮的变量名。封装成一个函数就是可以实现了。

放大像素会失真,如果你要实现这一共能的话可以用JLabel来显示。有一个方法可以实现的缩放ImageIcon ii = new ImageIcon("img/itemjpg");

Image img = iigetImage();
img = imggetScaledInstance(100, 100, ImageSCALE_DEFAULT);
ii = new ImageIcon(img);这个例子的getScaledInstance方法可以生成一个新的Image对象,可以缩放成指定的大小。

用 BufferedImage sourceImg = javaximageioImageIOread(in);可得到宽高 sourceImggetWidth()
sourceImggetHeight()
File file=new File(path);
ImageIOwrite(sourceImg, 后缀, file);
filelength();
将的文件转为File类型,有length方法可得到文件大小,格式就看文件名的后缀了

根据你的鼠标移动事件,判断你第一次点击的点和最后一次的点,就可以算出这个句型区域的长和宽了,
下面代码自己看
package comitheimautil;
import javaawtImage;
import javaawtimageBufferedImage;
import javaioFile;
import javaioFileOutputStream;
import javaioIOException;
import comsunimagecodecjpegJPEGCodec;
import comsunimagecodecjpegJPEGImageEncoder;
/
制作缩略图

@author seawind

/
public class PicUtils {
private String srcFile;
private String destFile;
private int width;
private int height;
private Image img;
/
构造函数

@param fileName
String
@throws IOException
/
public PicUtils(String fileName) throws IOException {
File _file = new File(fileName); // 读入文件
thissrcFile = fileName;
// 查找最后一个
int index = thissrcFilelastIndexOf("");
String ext = thissrcFilesubstring(index);
thisdestFile = thissrcFilesubstring(0, index) + "_s" + ext;
img = javaximageioImageIOread(_file); // 构造Image对象
width = imggetWidth(null); // 得到源图宽
height = imggetHeight(null); // 得到源图长
}
/
强制压缩/放大到固定的大小

@param w
int 新宽度
@param h
int 新高度
@throws IOException
/
public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,
BufferedImageTYPE_INT_RGB);
_imagegetGraphics()drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodeccreateJPEGEncoder(out);
encoderencode(_image); // 近JPEG编码
outclose();
}
/
按照固定的比例缩放

@param t
double 比例
@throws IOException
/
public void resize(double t) throws IOException {
int w = (int) (width t);
int h = (int) (height t);
resize(w, h);
}
/
以宽度为基准,等比例放缩

@param w
int 新宽度
@throws IOException
/
public void resizeByWidth(int w) throws IOException {
int h = (int) (height w / width);
resize(w, h);
}
/
以高度为基准,等比例缩放

@param h
int 新高度
@throws IOException
/
public void resizeByHeight(int h) throws IOException {
int w = (int) (width h / height);
resize(w, h);
}
/
按照最大高度限制,生成最大的等比例缩略图

@param w
int 最大宽度
@param h
int 最大高度
@throws IOException
/
public void resizeFix(int w, int h) throws IOException {
if (width / height > w / h) {
resizeByWidth(w);
} else {
resizeByHeight(h);
}
}
/
设置目标文件名 setDestFile

@param fileName
String 文件名字符串
/
public void setDestFile(String fileName) throws Exception {
if (!fileNameendsWith("jpg")) {
throw new Exception("Dest File Must end with \"jpg\"");
}
destFile = fileName;
}
/
获取目标文件名 getDestFile
/
public String getDestFile() {
return destFile;
}
/
获取原始宽度 getSrcWidth
/
public int getSrcWidth() {
return width;
}
/
获取原始高度 getSrcHeight
/
public int getSrcHeight() {
return height;
}
}

java要实现把一个大压缩到指定大小的且长宽比不变可以尝试以下 *** 作:

建立一个AffineTransform

AffineTransform(double m00, double m10, double m01, double m11, double m02, double m12)

转换矩阵,缩放比较简单(矩阵可以干很多事情,想做图像处理软件可以研究下)
[ x'] [ m00 m01 m02 ] [ x ] [ m00x + m01y + m02 ]
[ y'] = [ m10 m11 m12 ] [ y ] = [ m10x + m11y + m12 ]
[ 1 ] [ 0 0 1 ] [ 1 ] [ 1 ]

10倍比较难算(根号10啊,当然你想算也行),9倍好点(9的开方是3),m00为1/3,m01为0,m02为0,m10为0,m11为1/3,m12为0。

再建一个AffineTransformOp,把上面的转换传进去
AffineTransformOp(AffineTransform xform, int interpolationType)

最后调用AffineTransformOp的BufferedImage filter(BufferedImage src, BufferedImage dst) ,src传原,返回值就是想要的Image,注意是返回值,不是dst,不明白可以看下Java API


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/12716848.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-27
下一篇 2023-05-27

发表评论

登录后才能评论

评论列表(0条)

保存