我想在Java中执行此代码,但遇到此错误:

Exception in thread "main" java.lang.ClassCastException: java.awt.image.PixelInterleavedSampleModel cannot be cast to java.awt.image.SinglePixelPackedSampleModel


谁能帮我?

源代码是:

    BufferedImage cat = ImageIO.read(new File("E:\\11.jpg"));
    WritableRaster img=cat.getRaster();
    final SinglePixelPackedSampleModel sppsm;
    sppsm = (SinglePixelPackedSampleModel) img.getSampleModel();
    final int scan = sppsm.getScanlineStride();

最佳答案

img.getSampleModel()返回java.awt.image.PixelInterleavedSampleModel而不是SinglePixelPackedSampleModel。因此,您必须更改代码:

BufferedImage cat = ImageIO.read(new File("E:\\11.jpg"));
WritableRaster img=cat.getRaster();
final PixelInterleavedSampleModel sppsm;
sppsm = (PixelInterleavedSampleModel) img.getSampleModel();
final int scan = sppsm.getScanlineStride();

08-08 01:05