下你所需,载你所想!
IT技术源码资料下载网站

java编写电子相册,图片生成指定大小

:其他软件 2020-07-07 17:25:52

java编写电子相册,图片生成指定大小

一、前言
如图所示:目前代码实现了文件夹读取,缩略图展示这两个功能,剩余功能大家看界面也能猜到了,有生之年系列吧,下次想写再说吧。
二、准备
1.win10
2.eclise3.jdk1.8.0_2214.jar下载链接:htts:search.maven.orgrtifactnet.coobirdthumbnailator0.4.8jar
三、结构其中日志模块还没有写。
四、代码
1、启动main
ackageimageView.lyrics.main;imortjava.awt.EventQueue;imortimageView.lyrics.ui.ImageViewFrameUI;ublicclassMain{
ublicstaticvoidmain(String[]args){
EventQueue.invokeLater(newRunnable(){
ublicvoidrun(){
try{
newImageViewFrameUI();
}catch(Excetione){
e.rintStackTrace();
}
}
});
}
}2、service
ackageimageView.lyrics.service;imortjava.io.File;
imortjava.util.ArrayList;
imortjava.util.Locale;imortjavax.swing.JFileChooser;
imortjavax.swing.filechooser.FileNameExtensionFilter;
imortjavax.swing.filechooser.FileSystemView;
imortjavax.swing.te.DefaultMutableTeNode;imortimageView.lyrics.ui.AlbumTeUI;
imortimageView.lyrics.ui.ImageUI;
imortimageView.lyrics.util.ResizeImage;ublicclassAlbumService{
enumImageTye{
jg,jeg,ng,CR2
}
rivateAlbumTeUIalbumTeUI=AlbumTeUI.getInstance();
rivateImageUIimageUI=ImageUI.getInstance(); rivateStringowseFolder(){
拿到文件系统
FileSystemViewfsv=FileSystemView.getFileSystemView();
设置过滤器
FileNameExtensionFilterfileNameExtensionFilter=newFileNameExtensionFilter("ImageFile","jg","jeg","ng");
修改为英文
JFileChooser.setDefaultLocale(Locale.US);
实例化
JFileChooserfileChooser=newJFileChooser();
设置当前工作目录为桌面
fileChooser.setCurntDictory(fsv.getHomeDictory());
设置标头
fileChooser.setDialogTitle("select");
设置文件选择类型
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
移除所有文件过滤模式
fileChooser.moveChoosableFileFilter(fileChooser.getAccetAllFileFilter());
添加文件过滤
fileChooser.setFileFilter(fileNameExtensionFilter);
拿到操作结果
intsult=fileChooser.showOenDialog(null);
判断操作结果
if(sult==JFileChooser.APPROVE_OPTION){
打印所选文件路径
turnfileChooser.getSelectedFile().toString();
}
turnnull;
}
rivatevoidergodicImage(Stringath,ArrayList<String>list){
DefaultMutableTeNodeant=albumTeUI.addAlbum(ath);
Filefile=newFile(ath);
File[]temList=file.listFiles();
for(inti=0;i<temList.length;i++){
Stringtem=temList[i].toString();
if(temList[i].isFile()&am;&am;isImage(tem)){
albumTeUI.addImage(ant,tem);
StringoutPath="thumbnail\"+tem.substring(tem.lastIndexOf("\")+1,tem.lastIndexOf("."))+"Tem.jg";
ResizeImage.generateFixedSizeImage(tem,outPath);
imageUI.addImage(outPath);
}
if(temList[i].isDictory()){
ergodicImage(temList[i].getAbsolutePath(),list);
}
}
}
rivatebooleanisImage(Stringname){
Stringsuffix=name.substring(name.lastIndexOf(".")+1);
for(ImageTyetye:ImageTye.values()){
if(suffix.equalsIgnoCase(tye.toString())){
turntrue;
}
}
turnfalse;

}
ublicbooleanoenFolder(){
Stringath=owseFolder();
if(ath==null||ath.isEmty()){
turnfalse;
}
ArrayList<String>arrayList=newArrayList<String>();
ergodicImage(ath,arrayList);
turntrue;
}
ublicstaticvoidmain(String[]args){
TODOAuto-generatedmethodstub }}
3、界面
AlbumTeUI
ackageimageView.lyrics.ui;imortjavax.swing.JPanel;
imortjavax.swing.JTe;
imortjavax.swing.te.DefaultMutableTeNode;
imortjavax.swing.te.DefaultTeModel;imortjava.awt.BorderLayout;
imortjava.awt.Dimension;
imortjava.util.HashMa;
imortjava.util.Ma;
imortjavax.swing.JScrollPane;ublicclassAlbumTeUIextendsJPanel{ **
*
*
rivatestaticfinallongserialVersionUID=1L;
rivateDefaultMutableTeNoderoot;
ublicJTete;
rivateMa<DefaultMutableTeNode,String>ma=newHashMa<>();
rivatestaticfinalAlbumTeUIinstance=newAlbumTeUI();
ublicMa<DefaultMutableTeNode,String>getMa(){
turnma;
} ublicstaticAlbumTeUIgetInstance(){
turninstance;
} **
*Catetheanel.
*
rivateAlbumTeUI(){
root=newDefaultMutableTeNode("Album");
DefaultTeModelmodel=newDefaultTeModel(root);
setLayout(newBorderLayout(0,0));

JPanelanel=newJPanel();
add(anel,BorderLayout.CENTER);
anel.setLayout(newBorderLayout(0,0));

JScrollPanescrollPane=newJScrollPane();
anel.add(scrollPane);
anel.setPferdSize(newDimension(80,520));
te=newJTe();
scrollPane.setViewortView(te);
te.setModel(model);
}
ublicDefaultMutableTeNodeaddAlbum(Stringath){
Stringname=ath.substring(ath.lastIndexOf("\"));
DefaultMutableTeNodeant=newDefaultMutableTeNode(name);
root.add(ant);
turnant;
}
ublicvoidaddImage(DefaultMutableTeNodeant,Stringath){
DefaultMutableTeNodedefaultMutableTeNode=newDefaultMutableTeNode(ath.substring(ath.lastIndexOf("\")+1));
getMa().ut(defaultMutableTeNode,ath);
ant.add(defaultMutableTeNode);
}
ublicvoiddeleteImage(){
DefaultMutableTeNodeselectNode=(DefaultMutableTeNode)te.getLastSelectedPathComonent();
if((selectNode==null)||(selectNode.isRoot())){
turn;
}
DefaultTeModelmodel=(DefaultTeModel)te.getModel();
model.moveNodeFromPant(selectNode);
te.aint();
}
ublicStringoenImage(){
DefaultMutableTeNodeselectNode=(DefaultMutableTeNode)te.getLastSelectedPathComonent();
if((selectNode==null)||(selectNode.isRoot())){
turnnull;
}
Stringath=ma.get(selectNode);
turnath;
}}
ImageDisalyUI
ackageimageView.lyrics.ui;imortjavax.swing.JPanel;
imortjava.awt.BorderLayout;
imortjavax.swing.ImageIcon;
imortjavax.swing.JButton;ublicclassImageDisalyUIextendsJPanel{ **
*
*
rivatestaticfinallongserialVersionUID=1L;
ublicJButtonbtnNewButton;
rivateImageIconimageIcon; **
*Catetheanel.
*
ublicImageDisalyUI(StringimagePath){
imageIcon=newImageIcon(imagePath);
setSize(imageIcon.getIconWidth(),imageIcon.getIconHeight());
setLayout(newBorderLayout(0,0));
btnNewButton=newJButton("");
btnNewButton.setIcon(imageIcon);
add(btnNewButton);
}
ublicImageIcongetImageIcon(){
turnimageIcon;
}}
ImageUI
ackageimageView.lyrics.ui;imortjava.awt.Dimension;imortjavax.swing.JPanel;ublicclassImageUIextendsJPanel{ **
*
*
rivatestaticfinallongserialVersionUID=1L;
rivatestaticfinalImageUIinstance=newImageUI();
rivateintositionX=5;
rivateintositionY=5;
ublicstaticImageUIgetInstance(){
turninstance;
}
**
*Catetheanel.
*
rivateImageUI(){
setLayout(null);
} ublicvoidaddImage(Stringath){
ImageDisalyUIimageDisalyUI=newImageDisalyUI(ath);
intwidth=this.getWidth()-80;
intheight=this.getHeight()-80;
intimageWidth=imageDisalyUI.getWidth();
intimageHeight=imageDisalyUI.getHeight();
imageDisalyUI.setBounds(ositionX,ositionY,imageWidth,imageHeight);
intx=imageWidth+ositionX;
inty=imageHeight+ositionY;
if(x<width&am;&am;y<height){
ositionX+=imageWidth+5;
}
if(x>width&am;&am;y<height){
ositionX=5;
ositionY+=imageHeight+5;
}
if(x>width&am;&am;y>height){
ositionY+=imageHeight;
this.setPferdSize(newDimension(ositionX,ositionY));
}
this.add(imageDisalyUI);
this.udateUI();
}
}
ImageViewFrameUI
ackageimageView.lyrics.ui;imortjava.awt.event.MouseAdater;
imortjava.awt.event.MouseEvent;imortjavax.swing.JFrame;
imortjavax.swing.JMenuBar;
imortjavax.swing.JMenu;
imortjavax.swing.JMenuItem;
imortjavax.swing.JPanel;
imortjava.awt.BorderLayout;
imortjava.awt.Dimension;
imortjavax.swing.JScrollPane;
imortjavax.swing.JSlitPane;imortimageView.lyrics.service.AlbumService;ublicclassImageViewFrameUI{ rivateJFrameframe;
rivateJMenuItemmntmOenfolder;
rivateAlbumServicealbumService=newAlbumService();
rivateAlbumTeUIalbumTeUI=AlbumTeUI.getInstance(); **
*Catethealication.
*
ublicImageViewFrameUI(){
initialize();
addListener();
leftPanelListener();
} **
*Initializethecontentsoftheframe.
*
rivatevoidinitialize(){
frame=newJFrame();
frame.setVisible(true);
frame.setTitle("ImageView");
frame.setBounds(100,100,800,600);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOeration(JFrame.EXIT_ON_CLOSE);

JMenuBarmenuBar=newJMenuBar();
frame.setJMenuBar(menuBar);

JMenumnFile=newJMenu("File");
menuBar.add(mnFile);

JMenuItemmntmOenFile=newJMenuItem("oenfile");
mnFile.add(mntmOenFile);

mntmOenfolder=newJMenuItem("oenfolder");
mnFile.add(mntmOenfolder);

JMenumnEdit=newJMenu("Edit");
menuBar.add(mnEdit);

JMenuItemmntmAdd=newJMenuItem("add");
mnEdit.add(mntmAdd);

JMenuItemmntmModify=newJMenuItem("modify");
mnEdit.add(mntmModify);

JMenuItemmntmRemove=newJMenuItem("move");
mnEdit.add(mntmRemove);

JMenumnView=newJMenu("View");
menuBar.add(mnView);

JMenuItemmntmFind=newJMenuItem("find");
mnView.add(mntmFind);

JMenumnHel=newJMenu("Hel");
menuBar.add(mnHel);

JMenuItemmntmAbout=newJMenuItem("About");
mnHel.add(mntmAbout);
frame.getContentPane().setLayout(newBorderLayout(0,0));

JScrollPanescrollPane=newJScrollPane();
frame.getContentPane().add(scrollPane);
JPanelanel=newJPanel();
scrollPane.setViewortView(anel);
anel.setPferdSize(newDimension(760,520));
anel.setLayout(newBorderLayout(0,0));

JSlitPaneslitPane=newJSlitPane();
slitPane.setOneTouchExandable(true);
slitPane.setResizeWeight(0.2);
anel.add(slitPane);
AlbumTeUIalbumTeUI=AlbumTeUI.getInstance();
slitPane.setLeftComonent(albumTeUI);
ImageUIimageUI=ImageUI.getInstance();
slitPane.setRightComonent(imageUI);
}

rivatevoidaddListener(){
mntmOenfolder.addActionListener(event->{
albumService.oenFolder();
});

}
rivatevoidleftPanelListener(){
MouseAdatermouseAdater=newMouseAdater(){
@Override
ublicvoidmouseClicked(MouseEvente){
StringimagePath=albumTeUI.oenImage();
System.out.rintln(imagePath);
}
};
albumTeUI.te.addMouseListener(mouseAdater);
}
}
4.util
ackageimageView.lyrics.util;imortnet.coobird.thumbnailator.Thumbnails;
imortjava.io.FileInutStam;
imortjava.io.IOExcetion;
imortjava.io.InutStam;***
ublicclassResizeImage{
ublicstaticvoidmain(String[]args){
ResizeImage.generateFixedSizeImage("D:\download\毕业图片\IMG_3303.CR2","thumbnail\3.jg");
}**
*使用给定的图片生成指定大小的图片
*
ublicstaticStringgenerateFixedSizeImage(StringinutPath,StringoututPath){
try{
InutStaminutStam=newFileInutStam(inutPath);
Thumbnails.of(inutStam).size(80,80).toFile(oututPath);
turnoututPath;
}catch(IOExcetione){
System.out.rintln("原因:"+e.getMessage());
}
turn"";
}
}五、效果

TAG:

相关阅读

最新推荐