㈠ 請教java高手們,幫我寫一個java編寫的圖片瀏覽器,功能如下:有自動瀏覽功能,每隔幾秒圖片自動翻頁。用
//改編的,CopyOfImageViewer.java打開一個有圖片的文件夾就可瀏覽了。
//MP3播放相關庫到:http://www.javazoom.net/javalayer/sources.html下載
//將下載到的zip文件里的jl1.0.1.jar復制到JDK目錄下的jre/lib/ext/目錄里即可.
//將源代碼main方法里的playMp3("d:\bad.mp3");改成自己的地址,換種方法BMP是可以支持的,這里不行暫不討論。
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjavax.swing.*;
importjavazoom.jl.player.Player;
,Runnable{
JPanelbts;
JLabelpl;
jscrollPanejsp;
JButtoncf,start,next,prev,stop;
JFramef;
JFileChooserfc;
File[]sf;
intindex;
Threadauto;
booleanautoFlag;
intdelay=5*1000;
//這里就是GUI布局
CopyOfImageViewer(){
pl=newJLabel();
pl.setHorizontalAlignment(JLabel.CENTER);
jsp=newJScrollPane(pl);
start=newJButton("start");
next=newJButton(">");
prev=newJButton("<");
stop=newJButton("stop");
bts=newJPanel(newFlowLayout(FlowLayout.CENTER));
bts.add(start);
bts.add(prev);
bts.add(next);
bts.add(stop);
cf=newJButton("Selectapicturefolder");
fc=newJFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
f=newJFrame();
f.setDefaultCloseOperation(3);
f.getContentPane().add(cf,"North");
f.getContentPane().add(jsp,"Center");
f.getContentPane().add(bts,"South");
f.setSize(400,300);
f.setLocationRelativeTo(null);
f.setVisible(true);
//給按鈕加入事件偵聽器
start.addActionListener(this);
next.addActionListener(this);
prev.addActionListener(this);
stop.addActionListener(this);
cf.addActionListener(this);
auto=newThread(this);
auto.start();
}
publicstaticvoidmain(String[]args){
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}catch(Exceptione){
try{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(Exceptione2){}
}
newCopyOfImageViewer();
playMp3("d:\bad.mp3");
}
//簡單MP3播放
privatestaticvoidplayMp3(Stringfile){
try{
Playerp=newPlayer(newFileInputStream(file));
p.play();
}catch(Exceptione){}
}
//處理各按鍵事件
publicvoidactionPerformed(ActionEvente){
Objectsrc=e.getSource();
if(src==cf){
into=fc.showOpenDialog(f);
if(o==JFileChooser.APPROVE_OPTION){
sf=fc.getSelectedFile().listFiles(newFilenameFilter(){
//合法的文件後綴
String[]suf={".PNG",".GIF",".JPG",};
publicbooleanaccept(Filedir,Stringname){
name=name.toUpperCase();
for(inti=0;i<suf.length;i++)
if(name.endsWith(suf[i]))returntrue;
returnfalse;
}
});
if(sf.length>0){
index=0;
showPic();
}
}
}
if(sf==null||sf.length==0)return;
if(src==start)startB();
elseif(src==stop)stopB();
elseif(src==next)next();
elseif(src==prev)prev();
}
voidprev(){
index=--index<0?sf.length-1:index;
showPic();
}
voidnext(){
index=++index>sf.length-1?0:index;
showPic();
}
publicvoidrun(){
while(true){
if(sf!=null&&sf.length>0&&autoFlag){
try{Thread.sleep(delay);}catch(Exceptione){}
next();
}
try{Thread.sleep(100);}catch(Exceptione){}
}
}
privatevoidstopB(){
autoFlag=false;
}
privatevoidstartB(){
autoFlag=true;
}
//顯示圖片
privatevoidshowPic(){
if(sf==null||sf.length==0)return;
pl.setIcon(newImageIcon(sf[index].getAbsolutePath()));
System.out.println(sf[index].getAbsolutePath());
}
}
㈡ 在JAVA中,實現一個簡單的圖像瀏覽器,部署「上一張」、「下一張」兩個按鈕,點擊按鈕可前後翻閱圖片。
我這有一個!圖片你自己找!但必須改成1.jpg,2.jpg........你也可以用卡片布局!比較不錯!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class bmt1 implements ActionListener
{JFrame f;
JPanel p1;
JPanel p2;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
JLabel l1;
ImageIcon icon1;
ImageIcon icon2;
ImageIcon icon3;
ImageIcon icon4;
Boolean ture;
int n=0;
public bmt1()
{f=new JFrame("版面布局");
f.setSize(400,500);
f.setVisible(true);
Container con=f.getContentPane();
con.setLayout(new GridLayout(2,1));
p1=new JPanel();
p2=new JPanel();
con.add(p1);
con.add(p2);
b1=new JButton("第一張");
b2=new JButton("下一張");
b3=new JButton("前一張");
b4=new JButton("最後一張");
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
p2.setLayout(new FlowLayout());
l1=new JLabel();
p1.add(l1);
icon1=new ImageIcon("1.jpg");
icon2=new ImageIcon("2.jpg");
icon3=new ImageIcon("3.jpg");
icon4=new ImageIcon("4.jpg");
f.addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);}
});
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1)
{ n=1;
l1.setIcon(new ImageIcon("1.jpg"));}
if(e.getSource()==b2)
{ n=n+1;
if(n>4){n=1;}
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b3)
{ if(n<1){n=4;}
n=n-1;
String s=String.valueOf(n);
l1.setIcon(new ImageIcon(s+".jpg"));}
if(e.getSource()==b4)
{n=4;
l1.setIcon(new ImageIcon("4.jpg"));}
}
public static void main(String args[])
{new bmt1();}
}
㈢ java web網頁上顯示圖片
圖片不要中文名字;
圖片格式最好為.jpg,不過有的需要.png、.gif格式的圖片,有的是需要Flash支持的;
圖片的顯示也需要通過不同的瀏覽器測試,當然,如果編程,google和IE肯定是最優先測試的;
路徑不要有中文名稱,其實在項目中,圖片一般會放在一個單獨的叫image的文件夾下,這是一種習慣,我們直接寫上相對路徑,如果可以顯示一張圖片並且存在的話,那麼別的幾乎不會出錯;
調試過程,是腳本錯了,還是頁面的標簽錯誤,亦或是你的頁面代碼不規范,出現亂碼等眾多情況。
5個應該夠了~~ 你找找看是哪裡吧~~ 不會繼續問哦~
這樣可以么?
㈣ 用java編寫一個圖片瀏覽器
稍麻煩一點,
下午給出代碼
-------------------------------------------------------------
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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 ImageView extends JFrame {
private boolean isSingle = true;
private JPanel panel = new JPanel();
private CardLayout card = null;
private File[] files = null;
private int fileIndex = 0;
private JPanel single = new JPanel();
private JPanel multiple = new JPanel();
public ImageView() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setSize(600, 450);
setResizable(false);
getContentPane().setLayout(null);
card = new CardLayout(0, 0);
panel.setLayout(card);
panel.setBounds(12, 46, 570, 328);
getContentPane().add(panel);
panel.add(single, "single");
single.setLayout(new BorderLayout(0, 0));
panel.add(multiple, "multiple");
multiple.setLayout(new GridLayout(2, 3, 0, 0));
JButton btnBrowse = new JButton("Browse...");
btnBrowse.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
chooser.setMultiSelectionEnabled(true);
int returnVal = chooser.showOpenDialog(getContentPane());
if (returnVal == JFileChooser.APPROVE_OPTION) {
files = chooser.getSelectedFiles();
showPicture();
}
}
});
btnBrowse.setBounds(12, 10, 91, 21);
getContentPane().add(btnBrowse);
JButton btnSingle = new JButton("single");
btnSingle.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isSingle = true;
card.show(panel, "single");
showPicture();
}
});
btnSingle.setBounds(115, 10, 91, 21);
getContentPane().add(btnSingle);
JButton btnMultiple = new JButton("multiple");
btnMultiple.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
isSingle = false;
card.show(panel, "multiple");
showPicture();
}
});
btnMultiple.setBounds(218, 10, 91, 21);
getContentPane().add(btnMultiple);
JButton btnPrevious = new JButton("previous");
btnPrevious.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isSingle) {
fileIndex -= 1;
} else {
fileIndex -= 6;
}
showPicture();
}
});
btnPrevious.setBounds(92, 384, 91, 21);
getContentPane().add(btnPrevious);
JButton btnNext = new JButton("next");
btnNext.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (isSingle) {
fileIndex += 1;
} else {
fileIndex += 6;
}
showPicture();
}
});
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation((screenSize.width - getWidth()) / 2,
(screenSize.height - getHeight()) / 2);
btnNext.setBounds(423, 384, 91, 21);
getContentPane().add(btnNext);
setVisible(true);
}
private void showPicture() {
if (files == null || files.length == 0) {
return;
}
if (fileIndex > files.length || fileIndex < 0) {
fileIndex = 0;
}
PicturePanel canvas = null;
if (isSingle) {
if (single.getComponentCount() > 0) {
canvas = (PicturePanel) single.getComponent(0);
} else {
canvas = new PicturePanel();
single.add(canvas, BorderLayout.CENTER);
}
canvas.setPath(files[fileIndex].getPath());
canvas.repaint();
single.repaint();
} else {
for (int i = 0; i < 6; i++) {
if (files.length == fileIndex + i) {
break;
}
if (multiple.getComponentCount() > i) {
canvas = (PicturePanel) multiple.getComponent(i);
} else {
canvas = new PicturePanel();
multiple.add(canvas);
}
canvas.setPath(files[fileIndex + i].getPath());
canvas.repaint();
multiple.repaint();
}
}
panel.updateUI();
}
public static void main(String[] args) {
new ImageView();
}
}
class PicturePanel extends JPanel {
private Image image;
public PicturePanel() {
}
public void setPath(String filename) {
ImageIcon icon = new ImageIcon(filename);
this.image = icon.getImage();
}
public void paint(Graphics g) {
super.paint(g);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, 400, 300, this);
}
}
㈤ java實現圖片預覽功能,可以顯示縮列圖,具有上下頁的功能求技術支持
把圖片按照規定的比例壓縮,然後保存至FTP,列表讀取縮略圖,單擊顯示原圖。
/**
*壓縮圖片方法一(高質量)
*@paramoldFile將要壓縮的圖片
*@paramwidth壓縮寬
*@paramheight壓縮高
*@paramsmallIcon壓縮圖片後,添加的擴展名(在圖片後綴名前添加)
*@paramquality壓縮質量范圍:<i>0.0-1.0</i>高質量:<i>0.75</i>中等質量:<i>0.5</i>低質量:<i>0.25</i>
*@parampercentage是否等比壓縮若true寬高比率將將自動調整
*/
publicstaticvoidcompressImage(StringoldFile,intwidth,intheight,StringsmallIcon,
floatquality,booleanpercentage){
try{
Filefile=newFile(oldFile);
//驗證文件是否存在
if(!file.exists())
thrownewFileNotFoundException("找不到原圖片!");
//獲取圖片信息
BufferedImageimage=ImageIO.read(file);
intorginalWidth=image.getWidth();
intorginalHeight=image.getHeight();
//驗證壓縮圖片信息
if(width<=0||height<=0||!Pattern.matches("^[1-9]\d*$",String.valueOf(width))
||!Pattern.matches("^[1-9]\d*$",String.valueOf(height)))
thrownewException("圖片壓縮後的高寬有誤!");
//等比壓縮
if(percentage){
doublerate1=((double)orginalWidth)/(double)width+0.1;
doublerate2=((double)orginalHeight)/(double)height+0.1;
doublerate=rate1>rate2?rate1:rate2;
width=(int)(((double)orginalWidth)/rate);
height=(int)(((double)orginalHeight)/rate);
}
//壓縮後的文件名
StringfilePrex=oldFile.substring(0,oldFile.lastIndexOf('.'));
StringnewImage=filePrex+smallIcon+oldFile.substring(filePrex.length());
//壓縮文件存放位置
FilesavedFile=newFile(newImage);
//創建一個新的文件
savedFile.createNewFile();
//創建原圖像的縮放版本
Imageimage2=image.getScaledInstance(width,height,Image.SCALE_AREA_AVERAGING);
//創建數據緩沖區圖像
BufferedImagebufImage=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//創建一個Graphics2D
Graphics2Dg2=bufImage.createGraphics();
//重繪圖像
g2.drawImage(image2,0,0,width,height,null);
g2.dispose();
//過濾像素矩陣
float[]kernelData={
-0.125f,-0.125f,-0.125f,
-0.125f,2,-0.125f,-0.125f,
-0.125f,-0.125f};
Kernelkernel=newKernel(3,3,kernelData);
//按核數學源圖像邊緣的像素復制為目標中相應的像素輸出像素
ConvolveOpcOp=newConvolveOp(kernel,ConvolveOp.EDGE_NO_OP,null);
//轉換像素
bufImage=cOp.filter(bufImage,null);
FileOutputStreamout=newFileOutputStream(savedFile);
JPEGImageEncoderencoder=JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParamparam=encoder.getDefaultJPEGEncodeParam(bufImage);
//設置壓縮質量
param.setQuality(quality,true);
encoder.encode(bufImage,param);
out.close();
System.out.println(newImage);
}catch(Exceptione){
e.printStackTrace();
System.out.println("壓縮失敗!"+e.getMessage());
}
}