java无法读取图像
我需要在原始文件中不作更改地阅读以下内容。我试着这样做,但它抛出像一个错误,但图像不是由系统读取java无法读取图像,java,image,swing,Java,Image,Swing,我需要在原始文件中不作更改地阅读以下内容。我试着这样做,但它抛出像一个错误,但图像不是由系统读取 sun.awt.image.ImageFormatException: Unsupported color conversion request at sun.awt.image.JPEGImageDecoder.readImage(Native Method) at sun.awt.image.JPEGImageDecoder.produceImage(JPEGIm
sun.awt.image.ImageFormatException: Unsupported color conversion request
at sun.awt.image.JPEGImageDecoder.readImage(Native Method)
at sun.awt.image.JPEGImageDecoder.produceImage(JPEGImageDecoder.java:136)
at sun.awt.image.InputStreamImageSource.doFetch(InputStreamImageSource.java:269)
at sun.awt.image.ImageFetcher.fetchloop(ImageFetcher.java:205)
at sun.awt.image.ImageFetcher.run(ImageFetcher.java:169)
我尝试了以下代码
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package imagereader;
/**
*
* @author user
*/
//public class Main {
//
// /**
// * @param args the command line arguments
// */
// public static void main(String[] args) {
// // TODO code application logic here
// }
//
//}
public class ColorApp extends JFrame {
DisplayPanel displayPanel;
JButton brightenButton, darkenButton, contrastIncButton, contrastDecButton, reverseButton, resetButton;
public ColorApp() {
super();
Container container = getContentPane();
displayPanel = new DisplayPanel();
container.add(displayPanel);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 2));
panel.setBorder(new TitledBorder("Click a Button to Perform the Associated Operation and Reset..."));
brightenButton = new JButton("Brightness >>");
brightenButton.addActionListener(new ButtonListener());
darkenButton = new JButton("Darkness >>");
darkenButton.addActionListener(new ButtonListener());
contrastIncButton = new JButton("Contrast >>");
contrastIncButton.addActionListener(new ButtonListener());
contrastDecButton = new JButton("Contrast <<");
contrastDecButton.addActionListener(new ButtonListener());
reverseButton = new JButton("Negative");
reverseButton.addActionListener(new ButtonListener());
resetButton = new JButton("Reset");
resetButton.addActionListener(new ButtonListener());
panel.add(brightenButton);
panel.add(darkenButton);
panel.add(contrastIncButton);
panel.add(contrastDecButton);
panel.add(reverseButton);
panel.add(resetButton);
container.add(BorderLayout.SOUTH, panel);
addWindowListener(new WindowEventHandler());
setSize(displayPanel.getWidth(), displayPanel.getHeight() + 25);
show();
}
class WindowEventHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
public static void main(String arg[]) {
new ColorApp();
}
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JButton button = (JButton) e.getSource();
if (button.equals(brightenButton)) {
displayPanel.brightenLUT();
displayPanel.applyFilter();
displayPanel.repaint();
} else if (button.equals(darkenButton)) {
displayPanel.darkenLUT();
displayPanel.applyFilter();
displayPanel.repaint();
} else if (button.equals(contrastIncButton)) {
displayPanel.contrastIncLUT();
displayPanel.applyFilter();
displayPanel.repaint();
} else if (button.equals(contrastDecButton)) {
displayPanel.contrastDecLUT();
displayPanel.applyFilter();
displayPanel.repaint();
} else if (button.equals(reverseButton)) {
displayPanel.reverseLUT();
displayPanel.applyFilter();
displayPanel.repaint();
} else if (button.equals(resetButton)) {
displayPanel.reset();
displayPanel.repaint();
}
}
}
}
class DisplayPanel extends JPanel {
Image displayImage;
BufferedImage bi;
Graphics2D big;
LookupTable lookupTable;
DisplayPanel() {
setBackground(Color.black); // panel background color
loadImage();
setSize(displayImage.getWidth(this), displayImage.getWidth(this)); // panel
createBufferedImage();
}
public void loadImage() {
displayImage = Toolkit.getDefaultToolkit().getImage("C:/Documents and Settings/admin/My Documents/NetBeansProjects/Imagereader/src/imagereader/ne-1.jpg");
MediaTracker mt = new MediaTracker(this);
mt.addImage(displayImage, 1);
try {
mt.waitForAll();
} catch (Exception e) {
System.out.println("Exception while loading.");
}
if (displayImage.getWidth(this) == -1) {
System.out.println("No jpg file");
System.exit(0);
}
}
public void createBufferedImage() {
bi = new BufferedImage(displayImage.getWidth(this), displayImage.getHeight(this), BufferedImage.TYPE_INT_ARGB);
big = bi.createGraphics();
big.drawImage(displayImage, 0, 0, this);
}
public void brightenLUT() {
short brighten[] = new short[256];
for (int i = 0; i < 256; i++) {
short pixelValue = (short) (i + 10);
if (pixelValue > 255)
pixelValue = 255;
else if (pixelValue < 0)
pixelValue = 0;
brighten[i] = pixelValue;
}
lookupTable = new ShortLookupTable(0, brighten);
}
public void darkenLUT() {
short brighten[] = new short[256];
for (int i = 0; i < 256; i++) {
short pixelValue = (short) (i - 10);
if (pixelValue > 255)
pixelValue = 255;
else if (pixelValue < 0)
pixelValue = 0;
brighten[i] = pixelValue;
}
lookupTable = new ShortLookupTable(0, brighten);
}
public void contrastIncLUT() {
short brighten[] = new short[256];
for (int i = 0; i < 256; i++) {
short pixelValue = (short) (i * 1.2);
if (pixelValue > 255)
pixelValue = 255;
else if (pixelValue < 0)
pixelValue = 0;
brighten[i] = pixelValue;
}
lookupTable = new ShortLookupTable(0, brighten);
}
public void contrastDecLUT() {
short brighten[] = new short[256];
for (int i = 0; i < 256; i++) {
short pixelValue = (short) (i / 1.2);
if (pixelValue > 255)
pixelValue = 255;
else if (pixelValue < 0)
pixelValue = 0;
brighten[i] = pixelValue;
}
lookupTable = new ShortLookupTable(0, brighten);
}
public void reverseLUT() {
byte reverse[] = new byte[256];
for (int i = 0; i < 256; i++) {
reverse[i] = (byte) (255 - i);
}
lookupTable = new ByteLookupTable(0, reverse);
}
public void reset() {
big.setColor(Color.black);
big.clearRect(0, 0, bi.getWidth(this), bi.getHeight(this));
big.drawImage(displayImage, 0, 0, this);
}
public void applyFilter() {
LookupOp lop = new LookupOp(lookupTable, null);
lop.filter(bi, bi);
}
public void update(Graphics g) {
g.clearRect(0, 0, getWidth(), getHeight());
paintComponent(g);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2D = (Graphics2D) g;
g2D.drawImage(bi, 0, 0, this);
}
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包装图像阅读器;
/**
*
*@author用户
*/
//公共班机{
//
// /**
//*@param指定命令行参数
// */
//公共静态void main(字符串[]args){
////此处的TODO代码应用程序逻辑
// }
//
//}
公共类ColorApp扩展了JFrame{
显示面板显示面板;
JButton亮按钮、暗按钮、对比度按钮、对比度按钮、反转按钮、重置按钮;
公共应用程序(){
超级();
Container=getContentPane();
displayPanel=新的displayPanel();
container.add(显示面板);
JPanel面板=新的JPanel();
panel.setLayout(新网格布局(3,2));
panel.setOrder(新标题边框(“单击按钮执行相关操作并重置…”);
光亮按钮=新的JButton(“亮度>>”);
addActionListener(newbuttonListener());
暗按钮=新的JButton(“暗>>”;
addActionListener(newbuttonListener());
对比度按钮=新的JButton(“对比度>>”;
ContractInButton.addActionListener(新按钮Listener());
对比度decbutton=newjbutton(“对比度我认为您的图像是adobe cmyk或cmyk格式的,因此只有系统无法读取图像。
您可以尝试使用图像magick工具将cmyk转换为rgb。此工具可以从终端模式执行rin命令。因此,请使用此工具,它将解决您的问题
终端命令:
对于单个图像,请使用以下选项:
convert original.png-colorspace RGB output.jpg
对于批处理图像,请使用以下命令:
文件夹路径>mogrify-颜色空间RGB*.jpg这个JPEG有多大(以字节为单位)。@andrewhompson你(他)不能通过BufferedImage
类或ImageIO
?@Yoda阅读图像吗?如果你是说“。BufferedImage
类使用ImageIO
?“那么……可能吧。很多图像无法以一种方式读取,也无法以另一种方式读取。这种情况是发生在所有JPEG上,还是只发生在一个(或几个)JPEG上?如果是,请附加或链接这样的JPEG。切换到ImageIO
听起来是个好主意,因为它提供了更多的灵活性(基于插件)而且在出现问题时更容易预测…如果这没有帮助,您可以尝试使用my,它将读取内置读取器无法读取的大多数JPEG。将-synchronize flag
添加到imagemagick命令以始终刷新到文件。