I am using Vector to add elements in a table in a blackberry project. The font does not seem to change programmatically. I have tested it in different screen blackberry phones. In BOLD & CURVE it seems fine, but in large screen phones like 9810 torch, 9790 BOLD, it takes some default font which is very big. Even if I change the font of the phone through setup, it changes the font of LabelFields and TextFields but applying FontFamily font does not reflect on Vector elements.
I am attaching the screenshots from 9800 & 9810...In 9800 it appears fine, in 9810, it looks big
Try this
for (int x = 0; x < vector.size(); x++) {
FriendListObject b = (FriendListObject) vector.elementAt(x);
name_ = b.getf_name().toString();
}
TableRowManager row = new TableRowManager() {
public void paint(Graphics g) {
g.setBackgroundColor(0xa2b8c3);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(0xe5e7e7);
g.clear();
super.paint(g);
}
};
LabelField name= new LabelField(name_+" :", DrawStyle.ELLIPSIS);
name.setFont(Font.getDefault().derive(Font.PLAIN));
row.add(name);
same situation I am also faced then,I customised fontfamily for each resolution.for example
if (Display.getWidth()==480 && Display.getHeight()==360) {
_custHeadNews = new CustTextField(_newsHead,30,0x05235b,TextField.FOCUSABLE);
_custMainNews = new CustTextField(_newsMain,25,0x666666, RichTextField.FOCUSABLE);
_custMetadata = new CustTextField(_metaData,15,0x666666,TextField.FOCUSABLE);
}
else if (Display.getWidth()==320 && Display.getHeight()==240) {
_custHeadNews = new CustTextField(_newsHead,25,0x05235b,TextField.FOCUSABLE);
_custMainNews = new CustTextField(_newsMain,15,0x666666, RichTextField.FOCUSABLE);
_custMetadata = new CustTextField(_metaData,10,0x666666,TextField.FOCUSABLE);
}
bolded font size for different resolutions.
This issue was solved by making changes in RegionStyles. Initially we had given Font parameter as null. After changing it to appFont1 instead of null, the app font stopped overriding and works fine now.
RegionStyles style = new RegionStyles(BorderFactory.createSimpleBorder(new XYEdges(5,0,0,0), Border.STYLE_TRANSPARENT), appFont1, null,null, RegionStyles.ALIGN_LEFT, RegionStyles.ALIGN_MIDDLE);
Related
The below data matrix is being read well using Barcode Scanner, zxing mobile app. However, the same is not being read by zxing java library.
I have some image transformation code commented. Even transforming the image, rotation or scaling doesn't help.
Ideally, I would like to perform all possible image pre-processing programatically until decoded.
What is the logic the mobile app using, since am scanning the same image from the computer screen and it is working.
Please find below, the code am using for decoding.
public class BarcodeReader {
private static Map<DecodeHintType,Object> hintsMap;
public static void main(String...args){
BufferedImage before = null;
hintsMap = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);
hintsMap.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
hintsMap.put(DecodeHintType.POSSIBLE_FORMATS, EnumSet.allOf(BarcodeFormat.class));
//hintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
try
{
before = ImageIO.read(new File("C:/ocr.jpg"));
decode(before);
/* for(int i=1; i < 1000;i++){
AffineTransform transform = new AffineTransform();
double rad = (double)i/100;
double scale = (double)i/100;
System.out.println("rad "+scale);
//transform.rotate(rad, before.getWidth()/2, before.getHeight()/2);
transform.scale(scale, scale);
BufferedImage after = new BufferedImage(before.getWidth(), before.getHeight(), BufferedImage.TYPE_INT_ARGB);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
after = op.filter(before, after);
decode(after);
}*/
//tmpBfrImage = tmpBfrImage.getSubimage(200, 100, 800, 800);
}
catch (IOException tmpIoe)
{
tmpIoe.printStackTrace();
}
}
public static void decode(BufferedImage tmpBfrImage){
if (tmpBfrImage == null)
throw new IllegalArgumentException("Could not decode image.");
LuminanceSource tmpSource = new BufferedImageLuminanceSource(tmpBfrImage);
BinaryBitmap tmpBitmap = new BinaryBitmap(new HybridBinarizer(tmpSource));
MultiFormatReader tmpBarcodeReader = new MultiFormatReader();
Result tmpResult;
String tmpFinalResult;
try
{
if (hintsMap != null && ! hintsMap.isEmpty())
tmpResult = tmpBarcodeReader.decode(tmpBitmap, hintsMap);
else
tmpResult = tmpBarcodeReader.decode(tmpBitmap);
// setting results.
tmpFinalResult = String.valueOf(tmpResult.getText());
System.out.println(tmpFinalResult);
System.exit(0);;
}
catch (Exception tmpExcpt)
{
tmpExcpt.printStackTrace();
}
}
}
I had problems at multiple levels. I downloaded zxing source from github and debugged it.
The first problem was adding the below line as hints screws up the recognition hintsMap.put(DecodeHintType.PURE_BARCODE, Boolean.FALSE);
Looking at their source code for DataMatrixReader, there was a line doing this
if (hints != null && hints.containsKey(DecodeHintType.PURE_BARCODE))
So, irrespective of setting PURE_BARCODE true or false, it considers it as true. Ideally hints should not contain the key.
The second problem was with the way the detector for DataMatrix works.
The detector was identifying the 'L' by looking at the number of black and white transitions from each vertices. Ideally, the transitions from Top-Left to Bottom-Left and Bottom-Left to Bottom-Right should have 0 transitions.
However, since the line was drawn closer towards the outer edge of the box, the transitions were not becoming 0. I made changes to move it closer to the center of the Left and Bottom Black Lines. This means moving the vertical red line to the right and the bottom red line a bit upwards. I added a new method Correct Points, that makes the necessary correction. This correction works for me, ideally one should be making the correction a bit more smarter.
ResultPoint pointA = correctPoints(cornerPoints[0], Vertices.TOPLEFT);
ResultPoint pointB = correctPoints(cornerPoints[1], Vertices.BOTTOMLEFT);
ResultPoint pointC = correctPoints(cornerPoints[2], Vertices.TOPRIGHT);
ResultPoint pointD = correctPoints(cornerPoints[3], Vertices.BOTTOMRIGHT);
---
---
private ResultPoint correctPoints(ResultPoint point, Vertices vertice){
if(vertice.equals(Vertices.TOPLEFT))
return new ResultPoint(point.getX()+10, point.getY()+5);
else if(vertice.equals(Vertices.BOTTOMLEFT)){
return new ResultPoint(point.getX()+10, point.getY()-5);
}else if(vertice.equals(Vertices.TOPRIGHT)){
return new ResultPoint(point.getX(), point.getY()+10);
}else{
return new ResultPoint(point.getX()-10, point.getY()-5);
}
}
After making these changes, data matrix detection was working for images that were as bad as or even poorer than these.
I was having similar problems using ZXing to decode DataMatrix barcodes. From what I can see, ZXing doesn't traverse the entire image you send it, but rather starts from the middle and expands out until it has found a barcode. So, if the DataMatrix barcode isn't centered in the image, ZXing will not be able to reliably find it. I implemented (a rather slow) workaround that fixes this problem, by creating different cropped versions of the image:
My core decode method is similar to that of the original post. My image traversal logic is as follows:
// Read the original image
final BufferedImage image = ImageIO.read(...);
final int width = image.getWidth();
final int height = image.getHeight();
// Try detect codes using different sections of the image.
//
// +------+------+
// | ##|## |
// | ##|## |
// | ##|## |
// +------+------+
// | | |
// | | |
// | | |
// +------+------+
//
// We create 9 cropped versions of the image, with each cropped
// version being 1/4 of the original image. We traverse the
// original image from left-to-right, top-to-bottom, and create
// 9 sub-images that we try to decode in turn.
for (int i=0; i<3; i++) {
for (int j=0; j<3; j++) {
final int x = i * width / 4;
final int y = j * height / 4;
final BufferedImage crop = image.getSubimage(x, y, width / 2, height / 2);
decoded(crop);
}
}
I have problem with fonts lin LibGDX. I have three different fonts and parameters:
parameter_score = new FreeTypeFontGenerator.FreeTypeFontParameter();
//Some parameters..
font_score = generator.generateFont(parameter_score);
parameter_Big = new FreeTypeFontGenerator.FreeTypeFontParameter();
//Some parameters..
font_Big = generator.generateFont(parameter_Big);
parameter_Small = new FreeTypeFontGenerator.FreeTypeFontParameter();
//Some parameters..
font_Small = generator.generateFont(parameter_Small);
and it is very slow to generate fonts. When the app starts, i see black screen for about 3 seconds. I heard about method, when I generate fonts only first time, then I save it to some file, and when I lunch app next time, it will get generated fonts from file. But i dont know how to save, and load generated fonts. Do anyone know?
BitmapFontWriter
BitmapFontWriter is a class in gdx-tools which can write BMFont files from a BitmapFontData instance. This allows a font to be generated using FreeTypeFontGenerator, then written to a font file and PNG files. BitmapFontWriter has the benefit that it can be more easily run from scripts and can make use of FreeTypeFontGenerator's shadows and borders. Otherwise, the output is very similar to Hiero, though Hiero avoids writing a glyph image multiple times if different character codes render the same glyph.
Usage can look like this:
new LwjglApplication(new ApplicationAdapter() {
public void create () {
FontInfo info = new FontInfo();
info.padding = new Padding(1, 1, 1, 1);
FreeTypeFontParameter param = new FreeTypeFontParameter();
param.size = 13;
param.gamma = 2f;
param.shadowOffsetY = 1;
param.renderCount = 3;
param.shadowColor = new Color(0, 0, 0, 0.45f);
param.characters = Hiero.EXTENDED_CHARS;
param.packer = new PixmapPacker(512, 512, Format.RGBA8888, 2, false, new SkylineStrategy());
FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.absolute("some-font.ttf"));
FreeTypeBitmapFontData data = generator.generateData(param);
BitmapFontWriter.writeFont(data, new String[] {"font.png"},
Gdx.files.absolute("font.fnt"), info, 512, 512);
BitmapFontWriter.writePixmaps(param.packer.getPages(), Gdx.files.absolute("imageDir"), name);
System.exit(0);
}
});
https://github.com/libgdx/libgdx/wiki/Hiero#bitmapfontwriter
I want to write text on the screen for my game, for things like fps, random text for items and stuff. How can I write that text?
Is it possible without the Basic Game class? Isn't there a command like this g.drawString("Hello World", 100, 100);?
Update: this answer is now outdated, and does not work at all with the latest versions of LWJGL. Until I update this answer fully, I recommend that you look here: https://jvm-gaming.org/t/lwjgl-stb-bindings/54537
You could use the TrueType fonts feature in the Slick-util library.
Using a common font is easy, just create the font like this:
TrueTypeFont font;
Font awtFont = new Font("Times New Roman", Font.BOLD, 24); //name, style (PLAIN, BOLD, or ITALIC), size
font = new TrueTypeFont(awtFont, false); //base Font, anti-aliasing true/false
If you want to load the font from a .ttf file, it's a little more tricky:
try {
InputStream inputStream = ResourceLoader.getResourceAsStream("myfont.ttf");
Font awtFont = Font.createFont(Font.TRUETYPE_FONT, inputStream);
awtFont = awtFont.deriveFont(24f); // set font size
font = new TrueTypeFont(awtFont, false);
} catch (Exception e) {
e.printStackTrace();
}
After you have successfully created a TrueTypeFont, you can draw it like this:
font.drawString(100, 50, "ABC123", Color.yellow); //x, y, string to draw, color
For more information, you can look at the documentation for TrueTypeFont and java.awt.Font, and the Slick-Util tutorial I got most of this code from.
Try Making a BufferedImage of required size. Then get its Graphics and draw a String. Then Convert it to a ByteBuffer and render it in OpenGL.
String text = "ABCD";
int s = 256; //Take whatever size suits you.
BufferedImage b = new BufferedImage(s, s, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = b.createGraphics();
g.drawString(text, 0, 0);
int co = b.getColorModel().getNumComponents();
byte[] data = new byte[co * s * s];
b.getRaster().getDataElements(0, 0, s, s, data);
ByteBuffer pixels = BufferUtils.createByteBuffer(data.length);
pixels.put(data);
pixels.rewind();
Now pixels contains the required Image data you need to draw.
Use GL11.glTexImage2D() function to draw the byte buffer.
I'm trying to set the background color of my QR Code using iText into a transparent background, however it does not work. Shows only white bars and black background.
What i have done so far:
My Code Snippet:
PdfContentByte cb = writer.getDirectContent();
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
java.awt.Image qrImage = qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
Image finalImage = Image.getInstance(writer, qrImage, 1);
finalImage.setAbsolutePosition(positionX, positionY);
cb.addImage(finalImage);
I have already generated my QR code and produced a PDF, however, when using
qrcode.createAwtImage(Color.WHITE,new Color(0, 0, 0, 0));
It does not produce an alpha background, instead it only shows a black background color.
I have also tried:
java.awt.Image qrImage =
qrcode.createAwtImage(Color.WHITE,Color.OPAQUE);
But obviously, my arguments are incorrect.
Help will be most appreciated, i've been working on this for a day now.
I have also tried Graphics, Graphics2g, converting it into BufferedImage.
Changing the assignment of finalImage to the following works:
Image finalImage = Image.getInstance(qrImage, null)
I don't know why using the getInstance method that takes a PdfWriter as first argument ruins the transparency, though...
I would solve this problem like this:
BarcodeQRCode qrcode = new BarcodeQRCode("sample message on qr", 100, 100, null);
Image image = qrcode.getImage();
Image mask = qrcode.getImage();
mask.makeMask();
image.setImageMask(mask);
document.add(image);
There may be an AWT solution too, but I'm more familiar with native PDF solutions than with using an AWT workaround.
I am new to Java and would like to know how to set the font and font color to be used for the next text to be added to a SWT StyledText box.
So for example I have an application that defines "command" and "data" text and each is to be displayed in a different font/color. So let's say I've just added some "command" text. Now how do I set things up so that the next text which will be "data" text is displayed in a different font and color?
I've done a lot of googling, but nothing seems to be helping me.
P.S.: This can't be the most efficient way to do it:
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);
So all I've been able to come up with the following technique that does work,
int a = st.getCharCount();
Font font = new Font(shlProtruleModifier.getDisplay(), "Courier", 10, SWT.NORMAL);
StyleRange[] sr = new StyleRange[1];
sr[0] = new StyleRange();
st.append("\r\nWhat the heck?");
sr[0].start = a;
sr[0].length = st.getCharCount() - a;
sr[0].font = font;
sr[0].foreground = SWTResourceManager.getColor(SWT.COLOR_BLACK);
st.replaceStyleRanges(sr[0].start, sr[0].length, sr);