1

我正在开发一个 Java 应用程序,它从 dvi 图像采集卡捕获视频。我想实时渲染从这个图像采集卡获取的帧。但是我遇到了生产力问题——渲染速度很慢,我的电脑每秒只能渲染 5-6 帧。尽管该设备每秒最多可以捕获 40 帧。我分析了我的代码,发现 drawImage 方法相对较慢。如果我用 BMP 图像调用 drawImage 方法,我通过 ImageIO.readImage 加载,而不是使用 drawImage 绘制它需要 20 毫秒。有了图像,我从图像采集卡中得到,绘制它们需要 100 多毫秒。我研究了图像采集卡库的代码,发现 BufferedImage 是通过以下方式创建的——

    ColorModel cm;
    if (format == PixelFormat.RGB24) {
       cm = new ComponentColorModel(
            ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] {8,8,8},
            false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
    } else {
        throw new UnsupportedOperationException();
    }
    SampleModel sm = cm.createCompatibleSampleModel(width, height);
    DataBuffer db = new DataBufferByte(pixels, length);
    WritableRaster raster = Raster.createWritableRaster(sm, db, null);
    return new BufferedImage(cm, raster, false, null);

像素是一个字节数组,由图像采集卡提供。有什么方法可以不同地创建 BufferedImage,以更快地调用 drawImage 方法。我知道 ColorSpaces,我不需要保留正确的颜色空间。速度更重要。

提前致谢

4

1 回答 1

1

我对 jai 的 ImageIO API 的 tiff 实现有一个问题,听起来很相似(我不知道这是否有帮助)。

基本上,它将颜色模型转换为“单像素打包样本模型”:P

这不是我的代码,我不相信它,我前段时间在网上找到它,恐怕我不记得在哪里(我确实尝试搜索它,但没有找到合适的参考。

/*******************************************************************************
 *
 * It seems that SinglePixelPackedSampleModel is the only fast mode when a
 * color profile is converted. This is probably a bug (that has nothing to do
 * with bugs 4886071 and 4705399).
 * Note that grayscale images (TYPE_GRAY) are not converted.
 *
 ******************************************************************************/
public static BufferedImage convertToSinglePixelPackedSampleModel(BufferedImage image) {

    long time = System.currentTimeMillis();

    WritableRaster sourceRaster = image.getRaster();
    ColorModel colorModel = image.getColorModel();
    ICC_ColorSpace colorSpace = (ICC_ColorSpace) colorModel.getColorSpace();
    final SampleModel ssmd = sourceRaster.getSampleModel();

    if (colorSpace.getType() == ColorSpace.TYPE_GRAY) {

        logger.info(">>>> TYPE_GRAY, not converting");

    } else if (!(ssmd instanceof PixelInterleavedSampleModel)) {

        logger.info(">>>> sourceSampleModel is " + ssmd.getClass() + ", not converting");

    } else {

        PixelInterleavedSampleModel sourceSampleModel = (PixelInterleavedSampleModel) ssmd;
        int[] bitMasks = new int[]{0x00ff0000, 0x0000ff00, 0x000000ff};

        SinglePixelPackedSampleModel sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, image.getWidth(),
                image.getHeight(), bitMasks);

        WritableRaster destRaster = Raster.createWritableRaster(sampleModel, null);
        DataBufferInt destDataBuffer = (DataBufferInt) destRaster.getDataBuffer();
        int[] destBuffer = destDataBuffer.getData();
        int[] bandOffsets = sourceSampleModel.getBandOffsets();

        for (int i = 0; i < bandOffsets.length; i++) {
            bandOffsets[i] += ((-sourceRaster.getSampleModelTranslateX() * sourceSampleModel.getPixelStride())
                    - (sourceRaster.getSampleModelTranslateY() * sourceSampleModel.getScanlineStride()));
        }

        DataBuffer sourceDataBuffer = sourceRaster.getDataBuffer();

        if (sourceDataBuffer instanceof DataBufferUShort) {

            convertUShortDataBuffer(image, (DataBufferUShort) sourceDataBuffer, sourceSampleModel, bandOffsets, destBuffer);

        } else if (sourceDataBuffer instanceof DataBufferByte) {

            convertByteDataBuffer(image, (DataBufferByte) sourceDataBuffer, sourceSampleModel, bandOffsets, destBuffer);

        } else {

            throw new IllegalArgumentException("Cannot deal with " + sourceDataBuffer.getClass());

        }

        String sourceProfileName = getICCProfileName(colorSpace.getProfile());

        if (sourceProfileName.equals("Nikon sRGB 4.0.0.3001")) {
            logger.warn(">>>> Workaround #1094403: using sRGB instead of " + sourceProfileName);
            colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(ColorSpace.CS_LINEAR_RGB));
        }

        colorModel = new DirectColorModel(colorSpace, 24, bitMasks[0], bitMasks[1], bitMasks[2], 0, false, DataBuffer.TYPE_INT);
        image = new BufferedImage(colorModel, destRaster, false, null);
    }

    time = System.currentTimeMillis() - time;
    logger.info(">>>> convertToSinglePixelPackedSampleModel() completed ok in " + time + " msec");

    return image;
}

/**
 * @param image
 * @param sourceDataBuffer
 * @param sourceSampleModel
 * @param bandOffsets
 * @param destBuffer
 */
protected static void convertByteDataBuffer(BufferedImage image, DataBufferByte sourceDataBuffer,
        PixelInterleavedSampleModel sourceSampleModel, int[] bandOffsets, int[] destBuffer) {
    int base = 0;
    int i = 0;
    byte[] sourceBuffer = sourceDataBuffer.getData();
    int pixelStride = sourceSampleModel.getPixelStride();

    for (int y = 0; y < image.getHeight(); y++) {
        int j = base;

        for (int x = 0; x < image.getWidth(); x++) {
            int r = (sourceBuffer[j + bandOffsets[0]] & 0xff);
            int g = (sourceBuffer[j + bandOffsets[1]] & 0xff);
            int b = (sourceBuffer[j + bandOffsets[2]] & 0xff);

            destBuffer[i++] = (r << 16) | (g << 8) | b;
            j += pixelStride;
        }

        base += sourceSampleModel.getScanlineStride();
    }
}

protected static void convertUShortDataBuffer(BufferedImage image, DataBufferUShort sourceDataBuffer,
        PixelInterleavedSampleModel sourceSampleModel, int[] bandOffsets, int[] destBuffer) {
    int base = 0;
    int i = 0;
    short[] sourceBuffer = sourceDataBuffer.getData();

    for (int y = 0; y < image.getHeight(); y++) {
        int j = base;

        for (int x = 0; x < image.getWidth(); x++) {
            int r = (sourceBuffer[j + bandOffsets[0]] & 0xffff) >> 8;
            int g = (sourceBuffer[j + bandOffsets[1]] & 0xffff) >> 8;
            int b = (sourceBuffer[j + bandOffsets[2]] & 0xffff) >> 8;

            destBuffer[i++] = (r << 16) | (g << 8) | b;
            j += 3;
        }

        base += sourceSampleModel.getScanlineStride();
    }
}

//    public static ICC_Profile getICCProfile(RenderedImage image) {
//
//        ColorSpace colorSpace = image.getColorModel().getColorSpace();
//
//        if (colorSpace instanceof ICC_ColorSpace) {
//
//            ICC_ColorSpace iccColorSpace = (ICC_ColorSpace) colorSpace;
//
//            return iccColorSpace.getProfile();
//
//        }
//
//        return null;
//
//    }

public static String getICCProfileName(ICC_Profile profile) {

    if (profile == null) {
        return null;
    }

    byte[] xx = profile.getData(ICC_Profile.icSigProfileDescriptionTag);
    int offset = 12;
    int count;

    for (count = 1; xx[offset + count] != 0; count++) {
        ;
    }

    return new String(xx, 0, offset, count);

}

基本上,只需调用.convertToSinglePixelPackedSampleModel(image).

我将(相对较大的 TIFF 图像)的渲染时间从几分钟缩短到几秒钟:P

ps - 我认为这是我找到原始代码的地方http://www.koders.com/java/fidFE1D69AFE6930A514D5E189310AB10A3DFD43F78.aspx

于 2012-07-29T05:39:20.343 回答