JAVA: SVG to JPG converter - java

is there any libraries to convert SVG to JPG ? else than BATIK ?
i used BATIK but i found some problems like this exception :
java.lang.NullPointerException
at org.apache.batik.css.engine.CSSEngine.getCascadedStyleMap(CSSEngine.java:684)
at org.apache.batik.css.engine.CSSEngine.getComputedStyle(CSSEngine.java:755)
at org.apache.batik.bridge.CSSUtilities.getComputedStyle(CSSUtilities.java:96)
at org.apache.batik.bridge.CSSUtilities.convertDisplay(CSSUtilities.java:509)
at org.apache.batik.bridge.GVTBuilder.buildGraphicsNode(GVTBuilder.java:176)
at org.apache.batik.bridge.GVTBuilder.buildComposite(GVTBuilder.java:148)
at org.apache.batik.bridge.GVTBuilder.build(GVTBuilder.java:76)
at org.apache.batik.transcoder.SVGAbstractTranscoder.transcode(SVGAbstractTranscoder.java:165)
at org.apache.batik.transcoder.image.ImageTranscoder.transcode(ImageTranscoder.java:86)
at org.apache.batik.transcoder.XMLAbstractTranscoder.transcode(XMLAbstractTranscoder.java:132)
this is my code :
JPEGTranscoder transcoder = new JPEGTranscoder();
transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
"org.apache.crimson.parser.XMLReaderImpl");
transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY,
new Float(1.0));
TranscoderInput input = new TranscoderInput(new FileInputStream("C:/Taha/SmartPlannerNew/rectangles.svg"));
OutputStream ostream = new FileOutputStream("C:/Taha/SmartPlannerNew/out.jpg");
TranscoderOutput output = new TranscoderOutput(ostream);
try {
transcoder.transcode(input, output);
} catch (TranscoderException e) {
System.out.println("error***************************************************************************************************************************");
e.printStackTrace();
}
System.out.println("done.");
ostream.close();
System.exit(0);
can any one help me please ?

Batik is exactly what you are looking for, is totally stable, and there really is not much else.
You can read the handy official tutorial on using the transcoder portion of batik:
http://xmlgraphics.apache.org/batik/using/transcoder.html
If you are still having trouble, try posting the code that you are using that causes the error, and a complete stack trace. Somebody here will be able to help you.

my mistake was that i was using different versions of jar files i deleted all my jars and downloaded a new set of jars from the same

Related

.jpg out of .cgi with java (IP Webcam)

hy there, i hope i can explain my question:
i´ve an ip webcam and i want to read&save a .jpg file out of the path
webacm-ip-adr:8084/snapshot.cgi
i´ve little java experience and would like to program it in processing to keep it simple:
i´ve found this link:
https://www.java.net/node/702486
but its a slight overkill for me to understand it would be great if i can work with the 2 processing examples: web/loadingimages and net/httpClient
or do i make an logic mistake and its not solveable this way ?
You can use java lib "Apache Commons IO" to done it.
My Simple Code:
URL url = new URL("http://webacm-ip-adr:8084/snapshot.cgi");
InputStream input = url.openStream();
String jpg = "sample.jpg";
FileOutputStream output = new FileOutputStream(jpg);
IOUtils.copy(input, output);
Class "IOUtils" is a common tool for IO stream operation in commons-io jar.

How do i open a PDF file in Netbeans?

I want my application to open a pdf file when I click a dedicated button. How would i approach this? Also if I run the application from netbeans it shows the pdf but when compiled nothing comes up?
My code
private void showHelpMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
File f = new File("ana.pdf");
try {
Desktop.getDesktop().open(f);
} catch (Exception ex) {
System.out.println(ex);}
You can explicitly give the entire file path, which might solve your problem. Also the OS you are using must support the operation. This might help:
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File("C:\\Users\\klinks\\Documents\\pdf.pdf");
Desktop.getDesktop().open(myFile);
} catch (IOException e) {
// System probably doesn't have a default PDF program
}
}
Your code gets the file from the current directory. The file is there when you run it from netbeans, but the file is not there when you run it.
Unfortunately, there's no easy way to do this. I think the best idea would be write the documentation as HTML, put it on a server, and open the web browser (using Desktop.browse). If someone else has a better idea, please comment.

Do any Java OCR tools convert images of text into editable text files?

I'm working on a project that entails photographing text (from any hard copy of text) and converting that text into a text file. Then I'd like to use that text file to do some different things, such as provide hyperlinks to news articles or allow the user to edit the document.
The tool I've tried so far is Java OCR from sourceforge.net, which works fine on the images provided in the package. But when I photograph my own text, it doesnt work at all. Is there some training process I should be implementing? If so, does anybody know how to implement it? Any help will go a long way. Thank you!
I have a java application where I ended up deciding to use Tesseract OCR, and just call out to it using Runtime.exec(). Perhaps not quite the answer you need, but just in case you'd not considered it.
Edit + code added in response to comment reply
On a Windows installation I think I was able to use an installer, or unzip a ready made binary.
On a Linux server, I needed to compile Tesseract myself, but it's not too hard if you're used to that kind of thing (gcc); the only gotcha is that there's a dependency on Leptonica which also needs to be compiled.
// Tesseract can only handle .tif format, so we have to convert it
ImageIO.write( ImageIO.read( new java.io.File(file.getPath())), "tif", tmpFile[0]);
String[] tesseractCmd = new String[]{"tesseract", tmpFile[0].getAbsolutePath(), StringUtils.removeEnd(tmpFile[1].getAbsolutePath(), ".txt")};
final Process process = Runtime.getRuntime().exec(tesseractCmd);
try {
int exitValue = process.waitFor();
if(exitValue == 0) {
final String extractedText = SearchableTextExtractionUtils.extractPlainText(new FileReader(tmpFile[1]));
return extractedText;
}
throw new SearchableTextExtractionException(exitValue, Arrays.toString(tesseractCmd));
} catch (InterruptedException e) {
throw new SearchableTextExtractionException(e);
} finally {
process.destroy();
}

Problems writing jpeg2000 using imageio

I'm trying to convert a DICOM image in jpeg2000 using imageio as in the below code, the same procedure is explained in oracle documentation, but doesn't work! I don't understand what I'm doing wrong. The Java Advanced Image I/O library is installed into the JRE.
Using: ImageIO.getReaderFormatNames() and ImageIO.getWriterFormatNames() it can be verified that DICOM and JPEG2000 are supported!
There are no errors thrown, but it takes too long to write the file, and the output file is corrupted.
Thank you in advance...
public void convert2JPEG(File sourceFileName) throws IOException{
Iterator<ImageReader> iter = ImageIO.getImageReadersByFormatName("DICOM");
ImageReader reader = iter.next();
if(reader == null) {
log.error("Could not locate any Readers for the DICOM format image.");
return;
}
File sourceFile = new File (sourceFileName);
ImageInputStream iis = ImageIO.createImageInputStream(sourceFile);
BufferedImage bi;
try{
bi = ImageIO.read(iis);
File outputFile = new File("outputFileName");
String format = "jpeg 2000";
ImageIO.write(bi, format, outputFile);
} catch(Exception e){
log.info("ERROR: " + e);
}finally {
iis.close();
}
}
JAI Image IO does not support DICOM to my knowledge, but does support JPEG2000. Do note that there is no Windows 64-bit version of JAI (that may be an issue for you too). I am surprised it is not giving any kind of error.
However, I agree with Anders that the best course for converting DICOM would be to use a toolkit. I'd suggest DCM4CHE2 (http://www.dcm4che.org/confluence/display/d2/dcm4che2+DICOM+Toolkit). They have a number of command line tools for doing exactly what you are suggesting, and Dicom[Input/Output]Stream classes for reading and writing DICOM.

missing javax.swing

I'm trying to use the pdf library from jpedal, using the code snippet found here: http://www.jpedal.org/simple_image_example.php
/**instance of PdfDecoder to convert PDF into image*/
PdfDecoder decode_pdf = new PdfDecoder(true);
/**set mappings for non-embedded fonts to use*/
FontMappings.setFontReplacements();
/**open the PDF file - can also be a URL or a byte array*/
try {
decode_pdf.openPdfFile("C:/myPDF.pdf"); //file
//decode_pdf.openPdfFile("C:/myPDF.pdf", "password"); //encrypted file
//decode_pdf.openPdfArray(bytes); //bytes is byte[] array with PDF
//decode_pdf.openPdfFileFromURL("http://www.mysite.com/myPDF.pdf",false);
/**get page 1 as an image*/
//page range if you want to extract all pages with a loop
//int start = 1, end = decode_pdf.getPageCount();
BufferedImage img=decode_pdf.getPageAsImage(1);
/**close the pdf file*/
decode_pdf.closePdfFile();
} catch (PdfException e) {
e.printStackTrace();
}
But on this line:
decode_pdf.openPdfFile("C:/myPDF.pdf"); //file
Eclipse trows an error:
The type javax.swing.JPanel cannot be resolved. It is indirectly
referenced from required .class files
It seems as if I'm missing javax.swing.*
Intellisence does give me other javax.* options but not the swing class.
I already searched google for this but I had no luck finding a solution.
Any ideas?
Can't get any clearer than this:
http://www.jpedal.org/PDFblog/2011/09/java-is-not-android/
Appears that the library I wanted to use was not compatible with android at all.
Also not the last sentence:
that makes converting a Java PDF viewer to Android a major task.
Thanks for crushing that last bit of hope for me jpedal...
I doubt path is not resolved, Try this
decode_pdf.openPdfFile("C:\\myPDF.pdf");

Categories