1

我有几百个缩略图,我搜索了一种使用 JAI 合并它们的方法。此外,他们必须首先通过透视变换进行拉伸。我发现这个转换效果很好: Java Advanced Imaging API 中的快速透视转换

我已经编写了一些测试代码来使用 Graphics2D 对象做一些事情,但我想它在 JAI 中会更有效。

谢谢!

-马可

    PlanarImage img1 = PlanarImage.wrapRenderedImage(bufIn1);
    PlanarImage img2 = PlanarImage.wrapRenderedImage(bufIn2);

    int numBands = img1.getSampleModel().getNumBands();
    ColorSpace cs = ColorSpace.getInstance(numBands == 1 ?ColorSpace.CS_GRAY : ColorSpace.CS_sRGB);
    ColorModel cm =RasterFactory.createComponentColorModel(DataBuffer.TYPE_BYTE,cs, true, false,Transparency.BITMASK);

    SampleModel sm =RasterFactory.createComponentSampleModel(img1.getSampleModel(),DataBuffer.TYPE_BYTE,img1.getTileWidth(),img1.getTileHeight(),numBands + 1);
    ImageLayout il = new ImageLayout();
    il.setSampleModel(sm).setColorModel(cm);
    RenderingHints rh = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, il);

    ParameterBlock pb;
    Dimension d;
    WarpPerspective wp;
    RenderedOp srca;


  //make background for single image
    pb = new ParameterBlock();
    pb.add(new Float(img1.getWidth())).add(new Float(img1.getHeight()));
    pb.add(new Byte[]{new Byte((byte)0xFF)});
    RenderedOp alpha = JAI.create("constant", pb);



    pb = new ParameterBlock();
    pb.addSource(img1).addSource(img1);
    pb.add(alpha).add(alpha).add(Boolean.FALSE);
    pb.add(CompositeDescriptor.DESTINATION_ALPHA_LAST);
    srca = JAI.create("composite", pb, rh);
    d = new Dimension(img1.getWidth(),img1.getHeight());
    wp = new WarpPerspective(PerspectiveTransform.getQuadToQuad(
            0 , 0, d.width, 0, d.width, d.height, 0, d.height,
            0 , 0, d.width, 0, d.width, d.height/2, 0, d.height));
    pb = (new ParameterBlock()).addSource(srca);
    pb.add(wp);
    pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST)); //INTERP_BICUBIC
    PlanarImage i1 = (PlanarImage)JAI.create("warp",pb);



    pb = new ParameterBlock().addSource(img2).addSource(img2);
    pb.add(alpha).add(alpha).add(Boolean.FALSE);
    pb.add(CompositeDescriptor.DESTINATION_ALPHA_LAST);
    srca = JAI.create("composite", pb, rh);
    d = new Dimension(img1.getWidth(),img1.getHeight());
    wp = new WarpPerspective(PerspectiveTransform.getQuadToQuad(
            0 , 0, d.width, 0, d.width,   d.height, 0, d.height,
            0 , 0, d.width, 0, d.width*2, d.height, 0, d.height/2));
    pb = (new ParameterBlock()).addSource(srca);
    pb.add(wp);
    pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST)); //INTERP_BICUBIC
    PlanarImage i2 = (PlanarImage)JAI.create("warp",pb);

    BufferedImage masterImage = new BufferedImage(1000, 1000, BufferedImage.TYPE_4BYTE_ABGR);
    Graphics2D g = masterImage.createGraphics();
    g.setBackground(FastSurfaceImageRenderable.transparencyColor);
    g.clearRect(0, 0, subImage.getWidth(), subImage.getHeight());
    g.drawImage(i1.getAsBufferedImage(), 0, 0, 100, 100, null);
    g.drawImage(i2.getAsBufferedImage(), 50, 50, 200, 100, null);
    g.dispose();
4

0 回答 0