導航:首頁 > 編程語言 > java預覽圖片

java預覽圖片

發布時間:2023-09-16 02:27:54

『壹』 用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" >獲取

閱讀全文

與java預覽圖片相關的資料

熱點內容
新聞網站後台如何操作前台 瀏覽:539
在剪映app中怎麼查看視頻尺寸 瀏覽:9
linux文件成分包括 瀏覽:886
文件轉換免費的軟體 瀏覽:644
linuxwpsxlsx 瀏覽:482
小米手機怎麼上移動網路連接失敗怎麼辦 瀏覽:598
win10系統打開java 瀏覽:479
全日制編程什麼意思 瀏覽:447
筆記本創建區域網怎麼傳文件 瀏覽:871
怎樣查看id密碼 瀏覽:647
贛州極客晨星少兒編程怎麼樣 瀏覽:690
覺醒年代哪個app可以免費觀看 瀏覽:830
如何關閉win10觸摸屏幕 瀏覽:761
蘋果142不能傳文件 瀏覽:128
如何看歷史底部數據 瀏覽:230
怎麼在電腦上下軟體或安裝app 瀏覽:798
qq頭像電影截圖情侶 瀏覽:87
安卓的網路位置設置在哪 瀏覽:973
編程俠官網如何登錄 瀏覽:484
借貸王app怎麼樣 瀏覽:552

友情鏈接