『壹』 用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());
}
}
『叁』 java swing显示图片问题
publicclassImage{
privateJFramejf=newJFrame("");
publicstaticvoidmain(String[]args){
newImage().initial();
}
publicvoidinitial(){
jf.setLayout(newGridLayout(5,5));
for(inti=1;i<=5;i++){
for(intj=1;j<=5;j++){
inttemp=(i-1)*5+j;
Iconic1=newImageIcon("/D:/tupian/"+temp+".jpg");
JLabeljl2=newJLabel(ic1);
jf.add(jl2,i+"");
}
}
jf.setSize(500,500);
jf.setVisible(true);
}
}
代码是这样子了!你得指定个目录,目录下有25个图,并且命名为1.jpg.........25.jpg,实在不行你建个我打开的那个目录,也就是在盘建个tupian的文件夹,然后我把我测试的图也复杂给你,给个邮箱即OK,效果图如我插入的图片:
『肆』 请教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怎么显示本地图片
在面板上搞一个和面板一样大的JLabel
然后,通过JFileChooser获得路径,利用这个图片的路径,构建一个ImageIcon
最后,根据这个ImageIcon去给JLabel对象setIcon(ImageIcon对象);
具体地:
1.panel.add(label,BorderLayout.CENTER);
2.ImageIcon icon = new ImageIcon(url);
3.label.setIcon(icon);
下面的代码你把 .JPG改成BMP试试看,O(∩_∩)O~
package com.shlq.sample;
import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class ImagePane extends JPanel
{
JLabel jl = null;
ImageIcon img = null;
public ImagePane()
{
img = new ImageIcon( "E:\\Picture\\1.jpg ");
jl = new JLabel(img);
this.setLayout(new BorderLayout());
this.add(jl, BorderLayout.CENTER);
}
public static void main(String[] args)
{
JFrame test = new JFrame( "Image Pane ");
test.getContentPane().add(new ImagePane());
test.pack();
test.setVisible(true);
test.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
『陆』 如何用java实现图片预览功能,求代码
使用两种不同的方法实现图片预览功能
Java代码
<BODY>
<script language="javascript">
function ShowImage(path){
document.all.divShow.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = path;
}
function test(){
document.all.showimg.src=document.all.file1.value;
}
</script>
<INPUT style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 272px" type="file"onchange="ShowImage(this.value)">
<div id="divShow" style="FILTER:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image);WIDTH:274px;HEIGHT:100px">
<input type="file" id="file1" onchange="test()"><br/>
<img id="showimg" style="width:200px;height:200px;">
</BODY>
『柒』 Java获取本地图片,怎么在浏览器实现预览,注意是多张图片,通过io流的方式,,,具体实现求大神指教。
windows有自带视频播放 音频 播放 图片查看的、是一个空间、你上网络搜搜看、我忘了、很久没弄了、很简单的 传一个路劲就ok、路劲你就使用<input type="file" >获取