java.lang.IllegalArgumentException:缩放常量的数量不等于颜色或颜色/ alpha分量的数量 [英] java.lang.IllegalArgumentException: Number of scaling constants does not equal the number of of color or color/alpha components

查看:90
本文介绍了java.lang.IllegalArgumentException:缩放常量的数量不等于颜色或颜色/ alpha分量的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / *创建一个ARGB BufferedImage * / 
BufferedImage img =(BufferedImage)image; // ImageIO.read(imageSrc);
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage bi = new BufferedImage(w,h,BufferedImage.TYPE_4BYTE_ABGR_PRE);
图形g = bi.getGraphics();
g.drawImage(img,0,0,null);

/ *创建一个重新缩放滤镜操作,使图像50%不透明* /
float [] scales = {1f,1f,1f,1f};
float [] offset = new float [4];
RescaleOp rop = new RescaleOp(scales,offset,null);

/ *绘制图片,应用滤镜* /
g2d.drawImage(bi,rop,0,0);

来源链接: http://download.oracle.com/javase/tutorial/2d/images/drawimage.html



使用简单的图片可以正常工作,但使用照片(jpg等),我得到如下异常: IllegalArgumentException:
缩放常量的数量不是
等于颜色或
颜色/ alpha分量的数量

更清楚...这是我的改编的测试代码类。这个代码风格抛出异常...

  public class ImageTest extends JLabel {

public Image image ;
私有BufferedImage bImage;
ImageObserver imageObserver;
float [] scales = {1f,1f,1f,1f};
float [] offset = new float [4];
RescaleOp rop;
int width;
int height;

public ImageTest(ImageIcon icon){

width = icon.getIconWidth();
height = icon.getIconHeight();

this.image = icon.getImage();
this.imageObserver = icon.getImageObserver();

//this.bImage=(BufferedImage)image; //以前的版本(使异常?)...

this.bImage = new BufferedImage(
width,height,BufferedImage.TYPE_INT_ARGB);
this.bImage.createGraphics()。drawImage(
this.image,0,0,width,height,imageObserver);

rop = new RescaleOp(scales,offset,null);
this.repaint();
}

public void paintComponent(Graphics g){
super.paintComponent(g);

Graphics2D g2d =(Graphics2D)g;
g2d.drawImage(this.bImage,rop,0,0);
}

public void setRescaleOp(RescaleOp rop){
this.rop = rop;
}
} // class end

我不太确定异常来自于,所以我需要你的建议在哪里看?

我想这是IndexColorModel问题,但如果是这样,我不知道如何解决它...



赞赏任何有用的评论:)

Andrew

解决方案

使用下面的示例和 image ,我无法重现您描述的效果。



我很困惑, <$ c $ TYPE_4BYTE_ABGR_PRE 的c> BufferedImage 有一个 ComponentColorModel ,与之比较熟悉的 DirectColorModel ,但它是一个 IndexColorModel 无法重新调整。



使用 filter(),因为之前的版本错误地重复使用了 BufferedImage



附录:在另一个答案中, p>


我不想每次创建一个新的 BufferedImage 。我想使用 JSlider 来即时过滤图片。


您引用的示例通过创建<$在 SeeThroughComponent 构造函数中调用c $ c> BufferedImage once ,然后调整 RescaleOp 在滑块的更改处理程序中。它似乎相当响应。



附录:在对您的问题进行编辑时,您提到 IllegalArgumentException 可能出现在遇到具有 IndexColorModel的 。我可以使用例如 TYPE_BYTE_INDEXED 。您可以通过捕获异常并单独呈现它们来解决这些图像,如图所示这里


  import java.awt.EventQueue; 
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/ ** @see https://stackoverflow.com/questions/5838842 * /
public class RescaleOpTest extends JPanel {

public static final String LINK =
http://www.freeimagehosting.net/uploads/576c64ef7b.png;

public RescaleOpTest(){
this.setLayout(new GridLayout(1,0));
Image img = null;
尝试{
img = ImageIO.read(新网址(LINK));
// img = ImageIO.read(new File(image.jpg));
} catch(IOException ex){
ex.printStackTrace(System.err);
}
int w = img.getWidth(null);
int h = img.getHeight(null);
BufferedImage bi = new BufferedImage(
w,h,BufferedImage.TYPE_4BYTE_ABGR_PRE);
Graphics2D g = bi.createGraphics();
g.drawImage(img,0,0,null);
g.dispose();

创建一个rescale滤镜op,使图像变得75%不透明* /
float [] scales = {1f,1f,1f,0.75f};
float [] offset = new float [4];
RescaleOp rop = new RescaleOp(scales,offset,null);
bi = rop.filter(bi,null);
this.add(new JLabel(new ImageIcon(img)));
this.add(new JLabel(new ImageIcon(bi)));
}

private void display(){
JFrame f = new JFrame(RescaleOpTest);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);


public static void main(String [] args){
EventQueue.invokeLater(new Runnable(){
$ b $ @Override
public void run(){
new RescaleOpTest()。display();
}
});
}
}


I am writing aplha composite test app based on this example

/* Create an ARGB BufferedImage */
   BufferedImage img = (BufferedImage)image;//ImageIO.read(imageSrc);
   int w = img.getWidth(null);
   int h = img.getHeight(null);
   BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR_PRE);
   Graphics g = bi.getGraphics();
   g.drawImage(img, 0, 0, null);

   /* Create a rescale filter op that makes the image 50% opaque */
   float[] scales = { 1f, 1f, 1f, 1f };
   float[] offsets = new float[4];
   RescaleOp rop = new RescaleOp(scales, offsets, null);

   /* Draw the image, applying the filter */
   g2d.drawImage(bi, rop, 0, 0);

source link: http://download.oracle.com/javase/tutorial/2d/images/drawimage.html

It works fine with simple images but with photos (jpg etc) I get this exception like:

java.lang.IllegalArgumentException: Number of scaling constants does not equal the number of of color or color/alpha components

To be more clear... Here is my adapted test code class. This code style throws the exception...

public class ImageTest extends JLabel {

    public Image image;
    private BufferedImage bImage;
    ImageObserver imageObserver;
    float[] scales = {1f, 1f, 1f, 1f};
    float[] offsets = new float[4];
    RescaleOp rop;
    int width;
    int height;

    public ImageTest(ImageIcon icon) {

        width = icon.getIconWidth();
        height = icon.getIconHeight();

        this.image = icon.getImage();
        this.imageObserver = icon.getImageObserver();

        //this.bImage=(BufferedImage)image; //previous version (makes exception?)...

        this.bImage = new BufferedImage(
            width, height, BufferedImage.TYPE_INT_ARGB);
        this.bImage.createGraphics().drawImage(
            this.image, 0, 0, width, height, imageObserver);

        rop = new RescaleOp(scales, offsets, null);
        this.repaint();
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        Graphics2D g2d = (Graphics2D) g;
        g2d.drawImage(this.bImage, rop, 0, 0);
    }

    public void setRescaleOp(RescaleOp rop) {
        this.rop = rop;
    }
}//class end

I am not pretty sure where the exception comes from so I need your advice where to look at?

P.S. I suppose it is the IndexColorModel problem but if so I am not sure how to fix it...

Any useful comment is appreciated :)

Andrew

解决方案

Using the example below and the image you provided, I am unable to reproduce the effect you describe.

I was puzzled that a BufferedImage of TYPE_4BYTE_ABGR_PRE has a ComponentColorModel, in contrast to the more familiar DirectColorModel, but it's an IndexColorModel that cannot be rescaled.

Addendum: Updated code to use filter(), as the previous version was incorrectly reusing the BufferedImage.

Addendum: In another answer, you said,

I don't want to create a new BufferedImage each time. I want to filter the image on the fly using a JSlider.

The example you cited does this by creating the BufferedImage once in the SeeThroughComponent constructor and then adjusting just the RescaleOp in the slider's change handler. It seems quite responsive.

Addendum: In an edit to your question, you mention that the IllegalArgumentException may arise when encountering an image having an IndexColorModel. I am able to reproduce this using, e.g. TYPE_BYTE_INDEXED. You may be able to work around such images by trapping the exception and rendering them separately as shown here.

import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/** @see https://stackoverflow.com/questions/5838842 */
public class RescaleOpTest extends JPanel {

    public static final String LINK =
        "http://www.freeimagehosting.net/uploads/576c64ef7b.png";

    public RescaleOpTest() {
        this.setLayout(new GridLayout(1, 0));
        Image img = null;
        try {
            img = ImageIO.read(new URL(LINK));
//            img = ImageIO.read(new File("image.jpg"));
        } catch (IOException ex) {
            ex.printStackTrace(System.err);
        }
        int w = img.getWidth(null);
        int h = img.getHeight(null);
        BufferedImage bi = new BufferedImage(
            w, h, BufferedImage.TYPE_4BYTE_ABGR_PRE);
        Graphics2D g = bi.createGraphics();
        g.drawImage(img, 0, 0, null);
        g.dispose();

        /* Create a rescale filter op that makes the image 75% opaque */
        float[] scales = {1f, 1f, 1f, 0.75f};
        float[] offsets = new float[4];
        RescaleOp rop = new RescaleOp(scales, offsets, null);
        bi = rop.filter(bi, null);
        this.add(new JLabel(new ImageIcon(img)));
        this.add(new JLabel(new ImageIcon(bi)));
    }

    private void display() {
        JFrame f = new JFrame("RescaleOpTest");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new RescaleOpTest().display();
            }
        });
    }
}

这篇关于java.lang.IllegalArgumentException:缩放常量的数量不等于颜色或颜色/ alpha分量的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆