「java图片放缩」java压缩图片大小
本篇文章给大家谈谈java图片放缩,以及java压缩图片大小对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
我写的java图片查看器怎么让图片缩小放大呢?
放大像素会失真,如果你要实现这一共能的话可以用JLabel来显示图片。有一个方法可以实现图片的缩放ImageIcon
ii
=
new
ImageIcon("img/item.jpg");
Image
img
=
ii.getImage();
img
=
img.getScaledInstance(100,
100,
Image.SCALE_DEFAULT);
ii
=
new
ImageIcon(img);这个例子的getScaledInstance方法可以生成一个新的Image对象,可以缩放成指定的大小。
java图片缩放
根据你的鼠标移动事件,判断你第一次点击的点和最后一次的点,就可以算出这个句型区域的长和宽了,
下面代码自己看
package com.itheima.util;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 制作图片缩略图
*
* @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); // 读入文件
this.srcFile = fileName;
// 查找最后一个.
int index = this.srcFile.lastIndexOf(".");
String ext = this.srcFile.substring(index);
this.destFile = this.srcFile.substring(0, index) + "_s" + ext;
img = javax.imageio.ImageIO.read(_file); // 构造Image对象
width = img.getWidth(null); // 得到源图宽
height = img.getHeight(null); // 得到源图长
}
/**
* 强制压缩/放大图片到固定的大小
*
* @param w
* int 新宽度
* @param h
* int 新高度
* @throws IOException
*/
public void resize(int w, int h) throws IOException {
BufferedImage _image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
_image.getGraphics().drawImage(img, 0, 0, w, h, null); // 绘制缩小后的图
FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
encoder.encode(_image); // 近JPEG编码
out.close();
}
/**
* 按照固定的比例缩放图片
*
* @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 (!fileName.endsWith(".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实现图形的放大和缩小
java实现图形的放大和缩小,其实就是在画图时,改变图片的长和宽。以下代码参考一下:
import java.awt.Graphics;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;
public class App extends JFrame implements MouseListener, ActionListener {
int x = 0;
int y = 0;
File[] selectedFiles = null;
int fileIndex = 0;
int width = 200;
int height = 200;
public App() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(400, 300);
setResizable(false);
getContentPane().setLayout(null);
JPanel panel = new ImagePanel();
panel.setBounds(12, 40, 370, 218);
getContentPane().add(panel);
addMouseListener(this);
JButton btnBrowse = new JButton("Browse");
btnBrowse.addActionListener(this);
btnBrowse.setBounds(12, 9, 91, 21);
getContentPane().add(btnBrowse);
setVisible(true);
}
public static void main(String[] args) {
new App();
}
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG GIF Images", "jpg", "gif");
// 设置文件类型
chooser.setFileFilter(filter);
// 打开选择器面板
int returnVal = chooser.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
selectedFiles = chooser.getSelectedFiles();
repaint();
}
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
x = point.x;
y = point.y;
}
public void mouseReleased(MouseEvent e) {
Point point = MouseInfo.getPointerInfo().getLocation();
int thisX = point.x;
int thisY = point.y;
System.out.println("thisX=" + thisX + " " + "thisY=" + thisY);
if ((y - thisY 20 y - thisY 0)
|| (y - thisY 0 y - thisY -20)) {
// Y 在20范围内移动认为是水平移动
if (x thisX) {
// right
if (selectedFiles != null
fileIndex selectedFiles.length - 1) {
fileIndex++;
}
} else {
// left
if (selectedFiles != null fileIndex 0) {
fileIndex--;
}
}
} else {
if (x thisX) {
// 右下
width += 20;
height += 20;
} else {
// 左上
width -= 20;
height -= 20;
}
}
repaint();
}
class ImagePanel extends JPanel {
public void paint(Graphics g) {
super.paint(g);
if (selectedFiles != null) {
ImageIcon icon = new ImageIcon(selectedFiles[fileIndex]
.getPath());
g.drawImage(icon.getImage(), 0, 0, width, height, this);
}
}
}
}
java将图片按比例缩小
Image srcImg = ImageIO.read(new FileInputStream(fnSrc) );//取源图
int width = 600; //假设要缩小到600点像素
int height = srcImg.getHeight(null)*600/srcImg.getWidth(null);//按比例,将高度缩减
System.out.println("Width: "+srcImg.getWidth(null));// 这几行是调试用
System.out.println("Height: "+srcImg.getHeight(null));
System.out.println("Width2: "+width);
System.out.println("Height2: "+height);
Image smallImg =srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH);//缩小
java图片放缩的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java压缩图片大小、java图片放缩的信息别忘了在本站进行查找喔。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。