I am using zxing api for creating barcode. But while creating i am not able to write barcode content as label below the barcode.
output --
output required --
The code to generate these barcode are as such -
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
public class BarcodeTesting {
private static void wrtieToStream(BitMatrix bitMatrix) {
try {
MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream(new File("hello" + ".png")));
System.out.println( " Barcode Generated.");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private BitMatrix generateBitMatrix(String content, BarcodeFormat format, int width, int height) {
MultiFormatWriter writer = new MultiFormatWriter();
BitMatrix bitMatrix = null;
try {
bitMatrix = writer.encode(content, format, width, height);
} catch (WriterException e) {
e.printStackTrace();
}
return bitMatrix;
}
public static void main(String[] args) {
BarcodeTesting obj = new BarcodeTesting();
BarcodeFormat barcodeFormat = BarcodeFormat.QR_CODE;
BitMatrix bitMatrix = obj.generateBitMatrix("MY QR Code 123", barcodeFormat, 24, 24);
// String formatString = format.toString();
wrtieToStream(bitMatrix);
}
}
As the QRCode specifiaction does not provide an option to include the content into the image I do not think zxing or any other barcode generator does offer this functionality. You will have to add the text to the image on your own. Be aware that you have to leave enough white space around the QRCode. Your above image doesn't have enough whitespace at the bottom.
#tobltobs. I search a lot but didn't find any way in zxing api to do as i am expecting . So as per your suggestion i created a image in which i pasted my barcode after generating & reading it.Then i write barcode content as String below that image .
Barcode4j can be used to embed text along with the bar code.
This link has working java code.
Related
i have a problem with Apahe POI using XSLF to convert presentation slides to images. The issue is that sometimes there are missing elements on pictures, most of the time background template images or some template shapes.
For conversion im using following code:
package com.itpharma.videocall.poi;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
public class PPTX2PNG {
public static void main(String[] args) {
String prezName = "presentation";
try {
File fin = new File(prezName + ".pptx");
FileInputStream fis = new FileInputStream(fin);
XMLSlideShow ppt = new XMLSlideShow(fis);
Dimension pptSize = ppt.getPageSize();
System.out.println("Presentation size: " + pptSize.width + " x " + pptSize.height);
ppt.getSlides().forEach(slide -> {
System.out.println(slide.getSlideNumber() + ": " + slide.getTitle());
BufferedImage img = new BufferedImage(pptSize.width, pptSize.height, 1);
Graphics2D gfx = img.createGraphics();
gfx.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
gfx.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
gfx.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
gfx.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
gfx.setColor(Color.WHITE);
gfx.clearRect(0, 0, pptSize.width, pptSize.height);
gfx.fill(new Rectangle2D.Float(0, 0, pptSize.width, pptSize.height));
slide.draw(gfx);
try {
File fout = new File(prezName + "-slide-" + slide.getSlideNumber() + ".png");
FileOutputStream fos = new FileOutputStream(fout);
javax.imageio.ImageIO.write(img, "png", fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
});
ppt.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Im using POI 3.15 version (latest stable), Eclipse Oxygen on MacOS 10.12.2 for development (production would be on ubuntu VPS) and i dont know what is going wrong, if its my code or my system or POI library or my imports or i dont know.
I attached few images where i used the same presentation and only changed design templates. In upper part of image is PowerPoin screenshot and in lower part are generated images with missing parts. Third template did produce an error an no images.
First template:
Second template:
Third template:
With last template i get following error:
Exception in thread "main" java.lang.IllegalArgumentException: Relationship null doesn't start with this part /ppt/slides/slide1.xml
at org.apache.poi.openxml4j.opc.PackagePart.getRelatedPart(PackagePart.java:469)
at org.apache.poi.xslf.usermodel.XSLFShape$2.getPart(XSLFShape.java:392)
at org.apache.poi.xslf.usermodel.XSLFShape$2.getImageData(XSLFShape.java:400)
at org.apache.poi.sl.draw.DrawPaint.getTexturePaint(DrawPaint.java:135)
at org.apache.poi.sl.draw.DrawPaint.getPaint(DrawPaint.java:112)
at org.apache.poi.sl.draw.DrawTextParagraph.getAttributedString(DrawTextParagraph.java:530)
at org.apache.poi.sl.draw.DrawTextParagraph.breakText(DrawTextParagraph.java:235)
at org.apache.poi.sl.draw.DrawTextShape.drawParagraphs(DrawTextShape.java:158)
at org.apache.poi.sl.draw.DrawTextShape.getTextHeight(DrawTextShape.java:219)
at org.apache.poi.sl.draw.DrawTextShape.drawContent(DrawTextShape.java:102)
at org.apache.poi.sl.draw.DrawSimpleShape.draw(DrawSimpleShape.java:93)
at org.apache.poi.sl.draw.DrawSheet.draw(DrawSheet.java:67)
at org.apache.poi.sl.draw.DrawSlide.draw(DrawSlide.java:39)
at org.apache.poi.xslf.usermodel.XSLFSlide.draw(XSLFSlide.java:301)
at com.itpharma.videocall.poi.PPTX2PNG.lambda$0(PPTX2PNG.java:48)
at java.util.ArrayList.forEach(ArrayList.java:1249)
at com.itpharma.videocall.poi.PPTX2PNG.main(PPTX2PNG.java:32)
I have also tested with other templates and with some custom made presentations and there are almost every time missing some shapes or background.
I am developing a wizard page. In that page i am converting a file into String and displaying it in the text box
But I want to display the text in a java format
I have tried with the below code
import org.eclipse.jdt.core.ToolFactory;
import org.eclipse.jdt.core.formatter.CodeFormatter;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.IDocument;
import org.eclipse.text.edits.MalformedTreeException;
import org.eclipse.text.edits.TextEdit;
public class FormatterTest {
public static void main(String[] args) {
String code = pagehandlerContent; //the file which im getting and saving it in string
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
TextEdit textEdit = codeFormatter.format(CodeFormatter.K_COMPILATION_UNIT, code, 0, code.length(), 0, null);
IDocument doc = new Document(code);
try {
textEdit.apply(doc);
System.out.println(doc.get());
} catch (MalformedTreeException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
}
}
but I am getting null pointer exception at textEdit.apply(doc) line
Your CodeFormatter is null
CodeFormatter codeFormatter = ToolFactory.createCodeFormatter(null);
Note: I Cant write comment
I'm new to this kabeja package so please can some one provide code example or reading material to render PNG from DXF file using Java?
This is the sample code that generate PNG image from DXF file.
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import org.kabeja.dxf.DXFDocument;
import org.kabeja.parser.*;
import org.kabeja.parser.ParserBuilder;
import org.kabeja.svg.SVGGenerator;
import org.kabeja.xml.*;
public class MyClass {
public static void main(String[] args) {
MyClas x=new MyClas();
x.parseFile("C:\\Users\\Space\\Desktop\\test2.dxf");
}
public void parseFile(String sourceFile) {
try {
FileOutputStream o=new FileOutputStream("C:\\Users\\Space\\Desktop\\test2.png");
InputStream in = new FileInputStream(sourceFile);//your stream from upload or somewhere
Parser dxfParser = ParserBuilder.createDefaultParser();
dxfParser.parse(in, "");
DXFDocument doc = dxfParser.getDocument();
SVGGenerator generator = new SVGGenerator();
//org.xml.sax.InputSource out = SAXPNGSerializer;
SAXSerializer out = new org.kabeja.batik.tools.SAXPNGSerializer();
out.setOutput(o);
generator.generate(doc,out,new HashMap());
} catch (ParseException e) {
e.printStackTrace();
} catch (Exception ioe) {
ioe.printStackTrace();
}
}
}
Hope you get what you required :)
I want to have a program that reads metadata from an MP3 file. My program should also able to edit these metadata. What can I do?
I got to search out for some open source code. But they have code; but not simplified idea for my job they are going to do.
When I read further I found the metadata is stored in the MP3 file itself. But I am yet not able to make a full idea of my baby program.
Any help will be appreciated; with a program or very idea (like an algorithm). :)
The last 128 bytes of a mp3 file contains meta data about the mp3 file., You can write a program to read the last 128 bytes...
UPDATE:
ID3v1 Implementation
The Information is stored in the last 128 bytes of an MP3. The Tag
has got the following fields, and the offsets given here, are from
0-127.
Field Length Offsets
Tag 3 0-2
Songname 30 3-32
Artist 30 33-62
Album 30 63-92
Year 4 93-96
Comment 30 97-126
Genre 1 127
WARINING- This is just an ugly way of getting metadata and it might not actually be there because the world has moved to id3v2. id3v1 is actually obsolete. Id3v2 is more complex than this, so ideally you should use existing libraries to read id3v2 data from mp3s . Just putting this out there.
You can use apache tika Java API for meta-data parsing from MP3 such as title, album, genre, duraion, composer, artist and etc.. required jars are tika-parsers-1.4, tika-core-1.4.
Sample Program:
package com.parse.mp3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.mp3.Mp3Parser;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class AudioParser {
/**
* #param args
*/
public static void main(String[] args) {
String fileLocation = "G:/asas/album/song.mp3";
try {
InputStream input = new FileInputStream(new File(fileLocation));
ContentHandler handler = new DefaultHandler();
Metadata metadata = new Metadata();
Parser parser = new Mp3Parser();
ParseContext parseCtx = new ParseContext();
parser.parse(input, handler, metadata, parseCtx);
input.close();
// List all metadata
String[] metadataNames = metadata.names();
for(String name : metadataNames){
System.out.println(name + ": " + metadata.get(name));
}
// Retrieve the necessary info from metadata
// Names - title, xmpDM:artist etc. - mentioned below may differ based
System.out.println("----------------------------------------------");
System.out.println("Title: " + metadata.get("title"));
System.out.println("Artists: " + metadata.get("xmpDM:artist"));
System.out.println("Composer : "+metadata.get("xmpDM:composer"));
System.out.println("Genre : "+metadata.get("xmpDM:genre"));
System.out.println("Album : "+metadata.get("xmpDM:album"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (TikaException e) {
e.printStackTrace();
}
}
}
For J2ME(which is what I was struggling with), here's the code that worked for me..
import java.io.InputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.media.control.MetaDataControl;
import javax.microedition.midlet.MIDlet;
public class MetaDataControlMIDlet extends MIDlet implements CommandListener {
private Display display = null;
private List list = new List("Message", List.IMPLICIT);
private Command exitCommand = new Command("Exit", Command.EXIT, 1);
private Alert alert = new Alert("Message");
private Player player = null;
public MetaDataControlMIDlet() {
display = Display.getDisplay(this);
alert.addCommand(exitCommand);
alert.setCommandListener(this);
list.addCommand(exitCommand);
list.setCommandListener(this);
//display.setCurrent(list);
}
public void startApp() {
try {
FileConnection connection = (FileConnection) Connector.open("file:///e:/breathe.mp3");
InputStream is = null;
is = connection.openInputStream();
player = Manager.createPlayer(is, "audio/mp3");
player.prefetch();
player.realize();
} catch (Exception e) {
alert.setString(e.getMessage());
display.setCurrent(alert);
e.printStackTrace();
}
if (player != null) {
MetaDataControl mControl = (MetaDataControl) player.getControl("javax.microedition.media.control.MetaDataControl");
if (mControl == null) {
alert.setString("No Meta Information");
display.setCurrent(alert);
} else {
String[] keys = mControl.getKeys();
for (int i = 0; i < keys.length; i++) {
list.append(keys[i] + " -- " + mControl.getKeyValue(keys[i]), null);
}
display.setCurrent(list);
}
}
}
public void commandAction(Command cmd, Displayable disp) {
if (cmd == exitCommand) {
notifyDestroyed();
}
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
Right now i use iText to generate a pdf automatically.
And my problem is that when the content is really very large, i need to calculate the content's height and width, and then add new page...
this is really very inconvinent.
so I wonder whether or not there is a method like:
Document.add("a very very large article");
and after this , it will auto generate a pdf file ????
Thanks in advance !
The following creates a 9 page pdf without having to calculate height and width.
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld.pdf"));
document.open();
String text = "";
for (int i = 0; i < 10000; i++) {
text += "test";
}
document.add(new Paragraph(text));
} catch (DocumentException e) {
System.err.println(e.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
document.close();
}
}
a new page will be generated automaticly, when the content of the current page is full.