java如何转换图片格式,高分求助,急!! jdk里有专门的api你可以找找.忘记是啥了。好象是...Stream.class 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.liming.applet;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import javax.swing.*;import java.io.*;import java.net.*;import javax.imageio.*;public class Converting extends JFrame { JLabel promptLabel; JTextField prompt; JButton promptButton; JFileChooser fileChooser; JComboBox comboBox; JButton saveButton; public Converting() { super("Image Conversion"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container contentPane = getContentPane(); JPanel inputPanel = new JPanel(); promptLabel = new JLabel("Filename:"); inputPanel.add(promptLabel); prompt = new JTextField(20); inputPanel.add(prompt); promptButton = new JButton("Browse"); inputPanel.add(promptButton); contentPane.add(inputPanel, BorderLayout.NORTH); fileChooser = new JFileChooser(); promptButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { int returnValue = fileChooser.showOpenDialog(null); if (returnValue == JFileChooser.APPROVE_OPTION) { File selectedFile = fileChooser.getSelectedFile(); if (selectedFile != null) { prompt.setText(selectedFile.getAbsolutePath()); } } } }); JPanel outputPanel = new JPanel(); String writerFormats[] = ImageIO.getWriterFormatNames(); ComboBoxModel comboBoxModel = new DefaultComboBoxModel(writerFormats); comboBox = new JComboBox(comboBoxModel); outputPanel.add(comboBox); saveButton = new JButton("Save"); outputPanel.add(saveButton); saveButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { try { String name = prompt.getText(); File file = new File(name); if (file.exists()) { BufferedImage image = ImageIO.read(file.toURL()); if (image == null) { System.err.println( "Invalid input file format"); } else { String selection = (String ) comboBox.getSelectedItem(); String outputFilename = name + "." + selection; File outputFile = new File(outputFilename); boolean found = ImageIO.write(image, selection, outputFile); if (found) { JDialog window = new JDialog(); Container windowContent = window.getContentPane(); BufferedImage newImage = ImageIO.read( outputFile); JLabel label = new JLabel(new ImageIcon( newImage)); JScrollPane pane = new JScrollPane(label); windowContent.add(pane, BorderLayout.CENTER); window.setSize(300, 300); window .show(); } else { System.err.println("Error saving"); } } } else { System.err.println("Bad filename"); } } catch (MalformedURLException mur) { System.err.println("Bad filename"); } catch (IOException ioe) { System.err.println("Error reading file"); } } }); contentPane.add(outputPanel, BorderLayout.SOUTH); } public static void main(String args[]) { JFrame frame = new Converting(); frame.pack(); frame.show(); }} dreamno,你好,我看了你的程序,给了我一些启发,但是执行你的程序后,总是显示“Invalid input file format”,我选择的是tif格式的图片文件,要转换成jpg的格式,我选择bmp的都不行,请问为什么, public static boolean convert(String source,String target){ try { RenderedOp rop = JAI.create("fileload", source); OutputStream outStream = new FileOutputStream(target); BMPEncodeParam param = new BMPEncodeParam(); ImageEncoder imageEncoder = ImageCodec.createImageEncoder("BMP", outStream, param); imageEncoder.encode(rop); outStream.close(); } catch(Exception ex){ return false; } return true; } 需要jai_codec.jar,jai_core.jar这两个包。可以在:http://java.sun.com/products/java-media/jai/downloads/download-iio.html下在到.如果不行的话。你给我留个mail我给你发过去。 关于swing中的用DefaultTreeModel删除结点removeNodeFromParent over the personal data 如何在java上再进一步 内部类的运用问题!!! 急问:如何在具有多条生产线的车间平面图上显示出某辆生产车的位置? 用Jtable做数据管理的界面,不过当用户edit时,按add或update 关于java 2d的问题,请教各位大哥。 救急:如何向Oracle库中的long字段插入数据? 谁能给一段java写的生成缩略图的代码? 谈谈面向对象 为什么不接受我的打印任务啊? 内部类问题
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import javax.imageio.*;public class Converting
extends JFrame {
JLabel promptLabel;
JTextField prompt;
JButton promptButton;
JFileChooser fileChooser;
JComboBox comboBox;
JButton saveButton;
public Converting() {
super("Image Conversion");
setDefaultCloseOperation(EXIT_ON_CLOSE);
Container contentPane = getContentPane();
JPanel inputPanel = new JPanel();
promptLabel = new JLabel("Filename:");
inputPanel.add(promptLabel);
prompt = new JTextField(20);
inputPanel.add(prompt);
promptButton = new JButton("Browse");
inputPanel.add(promptButton);
contentPane.add(inputPanel, BorderLayout.NORTH);
fileChooser = new JFileChooser();
promptButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
if (selectedFile != null) {
prompt.setText(selectedFile.getAbsolutePath());
}
}
}
});
JPanel outputPanel = new JPanel();
String writerFormats[] = ImageIO.getWriterFormatNames();
ComboBoxModel comboBoxModel = new DefaultComboBoxModel(writerFormats);
comboBox = new JComboBox(comboBoxModel);
outputPanel.add(comboBox);
saveButton = new JButton("Save");
outputPanel.add(saveButton);
saveButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
String name = prompt.getText();
File file = new File(name);
if (file.exists()) {
BufferedImage image = ImageIO.read(file.toURL());
if (image == null) {
System.err.println(
"Invalid input file format");
}
else {
String selection = (String
) comboBox.getSelectedItem();
String outputFilename = name + "." + selection;
File outputFile = new File(outputFilename);
boolean found = ImageIO.write(image, selection,
outputFile);
if (found) {
JDialog window = new JDialog();
Container windowContent = window.getContentPane();
BufferedImage newImage = ImageIO.read(
outputFile);
JLabel label = new JLabel(new ImageIcon(
newImage));
JScrollPane pane = new JScrollPane(label);
windowContent.add(pane, BorderLayout.CENTER);
window.setSize(300, 300);
window
.show();
}
else {
System.err.println("Error saving");
}
}
}
else {
System.err.println("Bad filename");
}
}
catch (MalformedURLException mur) {
System.err.println("Bad filename");
}
catch (IOException ioe) {
System.err.println("Error reading file");
}
}
});
contentPane.add(outputPanel, BorderLayout.SOUTH);
} public static void main(String args[]) {
JFrame frame = new Converting();
frame.pack();
frame.show();
}
}
try {
RenderedOp rop = JAI.create("fileload", source);
OutputStream outStream = new FileOutputStream(target);
BMPEncodeParam param = new BMPEncodeParam();
ImageEncoder imageEncoder = ImageCodec.createImageEncoder("BMP",
outStream,
param);
imageEncoder.encode(rop);
outStream.close();
}
catch(Exception ex){
return false;
}
return true;
}
可以在:
http://java.sun.com/products/java-media/jai/downloads/download-iio.html
下在到.如果不行的话。你给我留个
mail我给你发过去。