Can not cast bytearrayinputstream to File

hello People,
I am trying to accept a image file from user and try to read its content for which i am getting the following error
java.io.ByteArrayInputStream cannot be cast to class org.camunda.bpm.engine.variable.value.FileValue (java.io.ByteArrayInputStream is in module java.base of loader ‘bootstrap’; org.camunda.bpm.engine.variable.value.FileValue is in unnamed module of loader ‘app’)
java.lang.ClassCastException: class java.io.ByteArrayInputStream cannot be cast to class org.camunda.bpm.engine.variable.value.FileValue (java.io.ByteArrayInputStream is in module java.base of loader ‘bootstrap’; org.camunda.bpm.engine.variable.value.FileValue is in unnamed module of loader ‘app’)

can anyone please help me in this.

Hi @shoebsayyed118,

have a look at the docs about handling file type varibales: Process Variables | docs.camunda.org

Hope this helps, Ingo

Thanks @Ingo_Richtsmeier
I have reached till extracting the variable in InputStream but I want to convert that InputStream into File object which is giving me the above error

Hi @shoebsayyed118 ,

could you please share the code how you try to achieve this?

Kind regards
Adagatiya

I’m getting the following error
16:59:50.789 [http-nio-8080-exec-8] ERROR org.camunda.bpm.engine.context - ENGINE-16004 Exception while closing command context: org/slf4j/bridge/SLF4JBridgeHandler
java.lang.NoClassDefFoundError: org/slf4j/bridge/SLF4JBridgeHandler

At line no 23


can you guys can please help me

Hi @shoebsayyed118 ,

because you posted another exception now, I assume you managed to solve the first.

This error occurs due to missing dependencies. Try to add “JUL to SLF4J Bridge” to your dependencies:

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>jul-to-slf4j</artifactId>
    <version>x.x.x</version>
</dependency>

More information on SLF4J and bridging legacy APIs can be found here: Log4j Bridge (slf4j.org)

Kind regards
Adagatiya

Thnaks @Adagatiya
that exception is resolved but now i am getting this exception

java.lang.RuntimeException: Unsupported image format. May need to install JAI Image I/O package.
https://java.net/projects/jai-imageio/

for the following code

Object fileData = delegateExecution.getVariable(“variableName”);
InputStream in = (ByteArrayInputStream)fileData;
File file = stream2file(in);
String fileContent = ImageCracker.crackImage(file);
System.out.println(fileContent);

can anyone please help me in this

From my understanding, the problem occurs in the following line:

String result = instance.doOCR(imageFile);

So, the problem is not a Camunda problem — and we can only provide limited help.
I don’t know how Tesseract determines the image format, maybe your imageFile is lacking a proper file extension? Have you tried saving the file and opening it? Can you see the image?

You can try using a BufferedImage instead of file, i.e., converting the ByteArrayInputStream directly to a BufferedImage and passing this image to Tesseract.

Thanks @StephanHaarmann
I did this
Object fileData = delegateExecution.getVariable(“variableName”);
InputStream in = (ByteArrayInputStream) fileData;
BufferedImage bi = ImageIO.read(in);

ITesseract instance = new Tesseract();
instance.setLanguage(“eng”);
instance.setDatapath(“tessdata”);
try {
String result = instance.doOCR(bi);
System.out.println(result);
} catch (TesseractException e) {
System.err.println(e.getMessage());
}

but it is not printing the text from image and printing something else
o
(‘/
/
L.’ -
; 3 E
| j
A /
7 4
, d /’ il B
]
2019.12.2321:14

Please help me in this

Regards
Shoeb Sayyed

Sorry, I’m not a Teseract/OCR expert and cannot really help you.
However, I’ve two suggestions to narrow down the error:

  • Store the image to a file to check whether it got corrupted when uploading/handling it in Camunda
  • Write a simple Java application without Camunda to check if Tesseract operates as expected.

If both the file looks fine and the output of your simple java application is the same as the result of your Java Delegate, the problem is a Tesseract problem. In this case, you can expect more help from the Tesseract community.

I had tried a simple java application without camunda and it worked fine then i tried this with camunda and got these error

I did the following:

  1. created a simple process model: tesseractExampleProcess.bpmn (3.9 KB)
  2. created an embedded form to upload an image
<form role="form" name="form">
  <div class="form-group">
    <label for="fileUploadField">Choose an Image to upload</label>
    <input type="file"
           id="fileUploadField"
           cam-variable-name="imageFile"
           cam-variable-type="File"
           class="form-control" />
  </div>
</form>
  1. Implemented a java delegate as follows:
public class OCRDelegate implements JavaDelegate {
  @Override
  public void execute(DelegateExecution execution) throws Exception {
    System.out.println("OCR delegate invoked");
    BufferedImage newBi = ImageIO.read((InputStream) execution.getVariable("imageFile"));
    ITesseract instance = new Tesseract(); 
    instance.setDatapath("C://Program Files//Tesseract-OCR//tessdata");
    try {
      String result = instance.doOCR(newBi);
      System.out.println(result);
      execution.setVariable("result", result);
    } catch (TesseractException e) {
      System.err.println(e.getMessage());
    }
  }
}
  1. As an input, I took a screenshot of your last post.

Everything worked as expected and Tesseract detected the text.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.