㈠ 请教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());
}
}