I need your suggestions and recommendations in the next task. I use libdmtx, which comes with a command line utility that reads image files for ECC200 data matrix barcodes, reads their contents, and writes decoded messages to standard output. I want to use this command line utility in my linux java program. I am using ubuntu linux. I installed libdmtx on my Linux machine. and when i call the command
dmtxread -n / home / admin / ab.tif
on linux terminal it immediately gives the decoded barcode value.
when I am going to call this command with the help of my java program, the code scripts when executing the command and dotn produce the result. it looks like the program is processing or freezing.
Below is the java code that calls the following command
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
public class Classtest {
public static void getCodes(){
try
{
Process p;
String command[]=new String[3];
command[0]="dmtxread";
command[1]="-n";
command[2]="/home/admin/ab.tif";
System.out.println("Command : "+command[0]+command[1]+command[2]);
p=Runtime.getRuntime().exec(command);
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line=reader.readLine();
if(line==null){
reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
line=reader.readLine();
System.out.print("Decoded :- "+line);
}else{
System.out.print("Error :- "+line);
}
System.out.println(p.waitFor());
}catch(IOException e1) {
e1.getMessage();
e1.printStackTrace();
}catch(InterruptedException e2) {
e2.getMessage();
e2.printStackTrace();
}
}
public static void main(String args[]){
getCodes();
}
}
Please tell friends where my code goes wrong.
I cite the following article but don't get any help
http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html?page=1
I beg you, friends! Thank!
Here is the new code in which I used the ProcessBuilder class, this code also produced the same result as above, the code that it hangs on the line Process process = pb.start ();
public class Test {
public static void main(final String[] args) throws IOException, InterruptedException {
List<String> commands = new ArrayList<String>();
commands.add("dmtxread");
commands.add("-n");
commands.add("/home/admin/ab.tif");
System.out.println(commands);
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Process process = pb.start();
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null){
System.out.println(line);
}
if (process.waitFor() == 0)
System.out.println("Success!");
System.exit(0);
System.err.println(commands);
System.err.println(out.toString());
System.exit(1);
}
}
Please help me solve this problem. Thanks you!