程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> ImageIcon圖像處理相關測試【一些特殊的處理方式】,imageicon圖像處理

ImageIcon圖像處理相關測試【一些特殊的處理方式】,imageicon圖像處理

編輯:JAVA綜合教程

ImageIcon圖像處理相關測試【一些特殊的處理方式】,imageicon圖像處理


/*************以下源碼通過測試******************************/

package cn.jason.ios.images;

import java.awt.FileDialog;
import java.awt.Image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
*
* ImageIcon圖像處理相關測試[一些特殊的處理方式]
*
*/
public class MyImageIcon extends JFrame{

/**
* 打開本地管理器對話框,選擇圖片文件
* @return 返回圖片絕對路徑
*/
private static String getLocalImageAbsolutePath(){
JFrame frame=new JFrame();
//創建對話框
FileDialog dialog=new FileDialog(frame);
//設置尺寸
dialog.setBounds(300, 100, 500, 450);
//打開對話框
dialog.setVisible(true);
//獲取返回的絕對路徑
StringBuilder absolutePath=new StringBuilder();
absolutePath.append(dialog.getDirectory()=="null"?"null":dialog.getDirectory());
absolutePath.append(dialog.getFile()=="null"?"null":dialog.getFile());
//過濾信息
if(absolutePath.toString().contains("null")){
return "NO-Path";
}
return absolutePath.toString();
}
/**
* 終止程序
*/
private static void exitPrograming(){
System.exit(0);
}
/**
* 獲取圖片二進制數據 :[1024*1024*10]:10M
* @param imagePath 圖片絕對地址
* @return 返回二進制數據
*/
private static byte[] getImageBytes(String imagePath){
//二進制對象
byte[] b=new byte[1024*1024*10];
//創建圖片文件
File imageFile=new File(imagePath);
//圖片流對象
try {
FileInputStream fis=new FileInputStream(imageFile);
//寫入
fis.read(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return b!=null?b:null;
}
/**
* 通過圖片二進制數據 獲取ImageIcon對象
* @param b 二進制數據
* @return ImageIcon對象
*/
private static ImageIcon getImageIcon(byte[] b){
return new ImageIcon(b);
}
/**
* 通過圖片地址 獲取ImageIcon對象
* @param filename 圖片地址
* @return ImageIcon對象
*/
private static ImageIcon getImageIcon(String filename){
return new ImageIcon(filename);
}
/**
* 通過圖片地址對象 獲取ImageIcon對象
* @param location URL地址對象
* @return ImageIcon對象
*/
private static ImageIcon getImageIcon(URL location){
return new ImageIcon(location);
}
/**
* 通過圖片對象 獲取ImageIcon對象
* @param image Image地址對象
* @return ImageIcon對象
*/
private static ImageIcon getImageIcon(Image image){
return new ImageIcon(image);
}
/**
* 調整ImageIcon的尺寸
* @param icon Icon對象
* @param width 調整後的寬度
* @param height 調整後的高度
* @return
*/
private static ImageIcon imageSizeCorrect(ImageIcon icon,
int width,
int height){
Image images=icon.getImage();
images=images.getScaledInstance(width, height, Image.SCALE_DEFAULT );
return new ImageIcon(images);
}


/**
* 程序入口
* @param params
*/
public static void main(String[] params) {

String path=getLocalImageAbsolutePath();
System.out.println("打開地址:"+path);
MyImageIcon testImages=new MyImageIcon();
testImages.setBounds(200,100, 500, 500);
JLabel jLabel=new JLabel();
jLabel.setBounds(100,100, 300, 300);
jLabel.setIcon(imageSizeCorrect(MyImageIcon.getImageIcon(MyImageIcon.getImageBytes(path)),300,300));
testImages.add(jLabel);
testImages.setVisible(true);

//結束程序
MyImageIcon.exitPrograming();
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved