The layer is converted to a smart object layer and then the smart object layer is replaced (Java)

How to convert the layer to a smart object layer and then replace the smart object layer. How to use smart object layer in java.

Reference Thread:

I am trying to convert layer to smart layer and it is throwing exception.

SmartObjectLayer sm = (SmartObjectLayer) layer1;
Added this: sm.replaceContents("D:/Vue-project/demo/images/dog.png");

Error: java.lang.NegativeArraySizeException: null


This Topic is created by mudassir.fayyaz using Email to Topic tool.

@jackson123

You are simply casting layer to SmartObjectLayer and this is not right. You can type cast only layer of type/instance SmartObjectLayer to SmartObjectLayer. You need to first check the type of layer and then type cast that.

if(layer instanceof SmartObjectLayer)
{
  SmartObjectLayer sm = (SmartObjectLayer) layer1;
  Added this: sm.replaceContents("D:/Vue-project/demo/images/dog.png");
}

for (int i=0,h=Psdimage.getLayers().length;i<h;i++){
Layer layer1 = Psdimage.getLayers()[i];
System.out.println(layer1.getName());
if (layer1 instanceof SmartObjectLayer) {
SmartObjectLayer sm = (SmartObjectLayer) layer1;
sm.replaceContents(“D:/Vue-project/demo/images/dog.png”);

            PngOptions options = new PngOptions();
            Psdimage.save(data + "package1111.png", options);
        }

    }
    File file = new File("images/package1111.png");
    FileInputStream in = new FileInputStream(file);
    byte[] bytes = new byte[in.available()];
    in.read(bytes, 0, in.available());
    return bytes;

还是报错 Error: java.lang.NegativeArraySizeException: null

@jackson123

Please provide the source PSD file reproducing the issue on your end.