java缩放图片代码,java对图片进行缩放
怎么用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代码放大或缩小图片不失真。
创新互联-专业网站定制、快速模板网站建设、高性价比太仓网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式太仓网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖太仓地区。费用合理售后完善,十年实体公司更值得信赖。
如何用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 图片缩放代码
直接给你一个类,直接套用就好了
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;
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对图片进行缩放
分享地址:http://hbruida.cn/article/hsigco.html