怎么用java代码放大或缩小图片不失真 。放大图像不会导致失真,而缩小图像将不可避免的失真 。
Java中也同样是这样 。
但java提供了4个缩放的微调选项 。
image.SCALE_SMOOTH
//平滑优先
image.SCALE_FAST//速度优先
image.SCALE_AREA_AVERAGING
//区域均值
image.SCALE_REPLICATE
//像素复制型缩放
image.SCALE_DEFAULT
//默认缩放模式
调用方法
Image
new_img=old_img.getScaledInstance(1024,
768,
Image.SCALE_SMOOTH);
得到一张缩放后的新图 。
如何用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(
"JPGGIF 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 - thisY20y - thisY0)
|| (y - thisY0y - thisY-20)) {
// Y 在20范围内移动认为是水平移动
if (xthisX) {
// right
if (selectedFiles != null
fileIndexselectedFiles.length - 1) {
fileIndex;
}
} else {
// left
if (selectedFiles != nullfileIndex0) {
fileIndex--;
}
}
} else {
if (xthisX) {
// 右下
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 图片缩放代码直接给图像收缩代码Java你一个类图像收缩代码Java,直接套用就好图像收缩代码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;
【图像收缩代码Java java 图片缩略图】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.createCompatibleWritableRaster(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;
}
}
图像收缩代码Java的介绍就聊到这里吧 , 感谢你花时间阅读本站内容,更多关于java 图片缩略图、图像收缩代码Java的信息别忘了在本站进行查找喔 。
推荐阅读
- 竞技视频游戏,竞技视频是什么
- go语言判断质数,go判断字符串是否为数字
- 为什么喜欢纪实拍摄,为什么喜欢纪实摄影
- 头条给什么人开直播,在头条开直播能赚钱吗
- crc函数c语言 c语言中creat函数
- 包含远程监控视频软件html5的词条
- 怎么禁止更改excel,怎么禁止更改电脑时间
- linux中大于号命令,linux终端出现大于号
- mysql怎么找到根目录 怎么找到mysql的位置