放大圖像不會導(dǎo)致失真,而縮小圖像將不可避免的失真。Java中也同樣是這樣。但java提供了4個縮放的微調(diào)選項。image.SCALE_SMOOTH //平滑優(yōu)先image.SCALE_FAST//速度優(yōu)先image.SCALE_AREA_AVERAGING //區(qū)域均值image.SCALE_REPLICATE //像素復(fù)制型縮放image.SCALE_DEFAULT //默認縮放模式調(diào)用方法Image new_img=old_img.getScaledInstance(1024, 768, Image.SCALE_SMOOTH);得到一張縮放后的新圖。怎么用java代碼放大或縮小圖片不失真。
成都網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、重慶網(wǎng)站建設(shè)公司、微信開發(fā)、小程序開發(fā)、集團企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。核心團隊均擁有互聯(lián)網(wǎng)行業(yè)多年經(jīng)驗,服務(wù)眾多知名企業(yè)客戶;涵蓋的客戶類型包括:陽臺護欄等眾多領(lǐng)域,積累了大量豐富的經(jīng)驗,同時也獲得了客戶的一致稱譽!
這個只是實現(xiàn)了移動,你參考以下吧 !
public class MoveImage {
static int x,y;
private static int num=0;
private static Icon icon=null;
public static void main(String[] args) throws Exception{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.getContentPane().setLayout(null);//這個要設(shè)置成 null
//圖片
icon = new ImageIcon("d:/test.gif");//d:/test.gif本地一張圖片
JLabel l = new JLabel(icon); //創(chuàng)建具有指定圖像的 JLabel 實例。
l.setSize(icon.getIconWidth(),icon.getIconHeight());//設(shè)置面板的寬度和高度
l.setBorder(BorderFactory.createLineBorder(Color.red));//給圖片加上紅色外框
f.getContentPane().add(l);
f.setSize(900,700);
f.setVisible(true);
l.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
}
});
l.addMouseMotionListener(new MouseMotionListener(){
public void mouseDragged(MouseEvent e) {
JLabel l = (JLabel)e.getSource();
l.setLocation(l.getX()+e.getX()-x,l.getY()+e.getY()-y);
}
public void mouseMoved(MouseEvent e) {}
});
}
放大像素會失真,如果你要實現(xiàn)這一共能的話可以用JLabel來顯示圖片。有一個方法可以實現(xiàn)圖片的縮放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對象,可以縮放成指定的大小。
我只實現(xiàn)了放大縮小功能,旋轉(zhuǎn)還有點問題package com.lzw;import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/*
* @功能:實現(xiàn)圖片的放大縮小與旋轉(zhuǎn)功能
* @日期:2011.6.29
*/
public class OperationOnPic extends JFrame{
private JPanel imageJpanel;
private JPanel southJpanel;
private JButton rorateButton;
private Mycanvas canvas ;
private int imgWidth, imgHeight;
private JSlider jSlide; //滑動塊組建
// private JScrollPane jScrollPane ;
Image img;
private static int num=0;
private static int newWidth=0;
private static int newHeigth=0;
public static boolean isroate=false;
public OperationOnPic(){
setTitle("圖片操作");
//設(shè)置窗體大小
setSize(800, 600);
//設(shè)置窗體顯示在中央位置
setLocationRelativeTo(null);
//URL imgUrl = OperationOnPic.class.getResource("2.jpg"); //獲得圖片路徑
//img = Toolkit.getDefaultToolkit().getImage(imgUrl);// 獲取圖片資源
ImageIcon image=new ImageIcon(OperationOnPic.class.getResource("2.jpg"));
img=image.getImage();
canvas=new Mycanvas();
//調(diào)用布局方法
init();
//設(shè)置窗體可見
setVisible(true);
}
public void init(){
setDefaultCloseOperation(this.EXIT_ON_CLOSE); //設(shè)置窗體可關(guān)閉
imageJpanel=new JPanel();
southJpanel=new JPanel();
imageJpanel.setLayout(new BorderLayout());
imageJpanel.add(canvas,BorderLayout.CENTER);
imageJpanel.add(getJSlider(),BorderLayout.SOUTH);
rorateButton=new JButton("圖片旋轉(zhuǎn)");
rorateButton.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
num+=5;
isroate=true;
canvas.repaint();
isroate=false;
}
});
southJpanel.add(rorateButton); //將按鈕添加到面板上
this.getContentPane().add(imageJpanel,BorderLayout.CENTER);
this.getContentPane().add(southJpanel,BorderLayout.SOUTH);
}
public JSlider getJSlider(){
if(jSlide==null){
jSlide=new JSlider(); //實例化一個滑動塊對象
jSlide.setMaximum(500); //設(shè)置滑動塊的最大取值
jSlide.setMinimum(1); //設(shè)置滑動塊的最小取值
jSlide.setValue(50); //設(shè)置滑動塊當前值
jSlide.addChangeListener(new ChangeListener(){ public void stateChanged(ChangeEvent e) {
// TODO Auto-generated method stub
canvas.repaint(); //重新繪制圖像
}
});
}
return jSlide;
}
public static void main(String[] args) {
new OperationOnPic();
}
class Mycanvas extends Canvas{
public void paint(final Graphics g){ //重寫paint
if(isroate==false){
imgWidth=img.getWidth(this);
imgHeight=img.getHeight(this);
float value=jSlide.getValue(); //取得滑動塊的值
newWidth=(int)(imgWidth*value/100);
newHeigth=(int)(imgHeight*value/100);
g.drawImage(img,0,0,newWidth,newHeigth,this);
g.dispose();
}else{
num+=5;
Graphics2D g2=(Graphics2D)g;
g2.rotate(Math.toRadians(num));
g2.drawImage(img,0,0,400,400,this);
g.dispose();
}
}
}
沒有代碼,準備一個隱藏的 JInternalFrame,當鼠標移到一個圖片縮略圖時,我們通過 JAI (Java Advanced Image, Oracle 網(wǎng)站有下載的 jar) 來把圖片縮放成一個 BufferedImage,然后再 myInternalFrame.getMyPreviewPanel().setImageIcon(myBufferedImage); 替換 JInternalFrame 中的 myPreviewPanel 的圖標,再把 JInternalFrame 顯示到前面來,并給它一個鼠標事件,當鼠標移出這個窗體或鼠標單擊時把 JInternalFrame 隱藏回到原來的縮略圖列表中。
下面是我預(yù)覽產(chǎn)品圖片時的代碼,類似的方法做成的,先縮放在內(nèi)存中得到一張 BufferedImage 圖片,再放到 Preview 預(yù)覽框中顯示出來。你這個局部放大的話,就是把原來的圖片通過 JAI 剪切出一個方形再來放大,應(yīng)該在 JAI 中也有這個吧,不過我沒找過,你在它的 API 文檔中找方法列出,應(yīng)該有的。
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");
//?設(shè)置文件類型
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范圍內(nèi)移動認為是水平移動
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);
}
}
}
}