放大圖像不會導致失真,而縮小圖像將不可避免的失真。Java中也同樣是這樣。但java提供了4個縮放的微調選項。image.SCALE_SMOOTH //平滑優(yōu)先image.SCALE_FAST//速度優(yōu)先image.SCALE_AREA_AVERAGING //區(qū)域均值image.SCALE_REPLICATE //像素復制型縮放image.SCALE_DEFAULT //默認縮放模式調用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一張縮放后的新圖。怎么用java代碼放大或縮小圖片不失真。
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比太倉網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式太倉網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋太倉地區(qū)。費用合理售后完善,十年實體公司更值得信賴。
java實現(xiàn)圖形的放大和縮小,其實就是在畫圖時,改變圖片的長和寬。以下代碼參考一下:
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);
}
}
}
}
直接給你一個類,直接套用就好了
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;
}
}
網(wǎng)站欄目:java縮放圖片代碼,java對圖片進行縮放
轉載源于:http://jinyejixie.com/article20/hsigco.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、標簽優(yōu)化、域名注冊、品牌網(wǎng)站設計、網(wǎng)站設計、做網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)