Java JRE built-in image for testing purposes - java

Is there an image within the JRE that can be used for testing purposes? I'm looking for either an Image, BufferImage or Icon object. I did find a PNG file in the JRE path that I am currently using but looking to see what others have found or are using.

Try the following. This code will generate a test image of any resolution. It does not use a built-in image but I think this will work best for you. Tweak as necessary to meet your needs.
static private Image createTestImage(final int resolution) {
final Image image = new BufferedImage(resolution, resolution, BufferedImage.TYPE_INT_ARGB);
final Graphics g = image.getGraphics();
final int points = (resolution * 72) / 96;
g.setColor(new Color(.42f, .42f, 1.0f, .5242f));
g.setFont(new Font("Dialog", Font.BOLD, points));
g.drawString("!X!", 2, points);
g.setColor(Color.BLACK);
g.drawOval(0, 0, image.getWidth(null) - 1, image.getHeight(null) - 1);
g.drawOval(11, 11, image.getWidth(null) - 23, image.getHeight(null) - 23);
g.drawOval(22, 22, image.getWidth(null) - 45, image.getHeight(null) - 45);
return image;
}
Using
Image image = createTestImage(1024);
Produces a hi res image like:
Using
Image image = createTestImage(64);
Produces a lo res image like:

Depending on the OS, there are a number of image files bundled with the JRE...
There are images in C:\Program Files\Java\jre7\lib\images\cursors on Windows, and on Linux I found:
denis#laptop:~/Programs/jdk1.7.0_11/jre$ find | grep png
./lib/deploy/mixcode_s.png
./lib/images/icons/sun-java.png
./lib/images/icons/sun-java_HighContrast.png
./lib/images/icons/sun-java_HighContrastInverse.png
./lib/images/icons/sun-java_LowContrast.png
... (many others) ...

Related

Imebra library shows completely gray image for transfer syntax 1.2.840.10008.1.2.1

Iam trying to use Imebra library to display DICOM images in android. Iam using version 5.0 of the library.
The bitmap shown is completely gray, transfer syntax for image is 1.2.840.10008.1.2.1.For other supported transfer syntax i.e JPEG it works fine.
Also I am unable to add VOILUT transform functionality as mentioned in documentation it gives error cons tructor not found for VOILUT.
Below is the code Iam using, VOILUT transform part is giving constructor not found. if i remove VOILUT transform part things work fine but for image with transfer syntax 1.2.840.10008.1.2.1 it shows completely grey image
private Bitmap fromDicom(String filePath, int frameNumber){
// have been applied).
Image dicomImage = loadedDataSet.getImageApplyModalityTransform(frameNumber);
// Use a DrawBitmap to build a stream of bytes that can be handled by the
// Android Bitmap class.
com.imebra.TransformsChain chain = new com.imebra.TransformsChain();
if( com.imebra.ColorTransformsFactory.isMonochrome(dicomImage.getColorSpace()))
{
// Retrieve the VOIs (center/width pairs)
com.imebra.VOIs vois = loadedDataSet.getVOIs();
if(!vois.isEmpty())
{
// Get the first VOI setting from the dataset
chain.addTransform(new VOILUT(vois.get(0)));
}
else
{
// The dataset does not have any VOI setting, find the optimal one
com.imebra.SWIGTYPE_p_imebra__VOIDescription voiDescription = VOILUT.getOptimalVOI(dicomImage, 0, 0, dicomImage.getWidth(), dicomImage.getHeight());
chain.addTransform(new VOILUT(voiDescription));
}
}
DrawBitmap drawBitmap = new DrawBitmap(chain);
Memory memory = drawBitmap.getBitmap(dicomImage, drawBitmapType_t.drawBitmapRGBA, 4);
// Build the Android Bitmap from the raw bytes returned by DrawBitmap.
Bitmap renderBitmap = Bitmap.createBitmap((int)dicomImage.getWidth(), (int)dicomImage.getHeight(), Bitmap.Config.ARGB_8888);
byte[] memoryByte = new byte[(int)memory.size()];
memory.data(memoryByte);
ByteBuffer byteBuffer = ByteBuffer.wrap(memoryByte);
renderBitmap.copyPixelsFromBuffer(byteBuffer);
// Update the image
return renderBitmap;
}
After changing the code suggested by you, I don't find classes mentioned
VOIDescription instead i see class SWIGTYPE_p_imebra__VOIDescription should i use that class
There is one more error no getWidth() method available with vois.get(0).getWidth
One last Error i don't see class vois_t instead there is a class VOIs should VOIs be used
Thanks for the reponse
The VOILUT must be initialized with the proper contrast settings from the dataset like in the code below.
However, the dataset contains a VOI setting that is wrong (the window width is 0) so this file will be displayed correctly only if you use custom VOI settings or just use automatic settings when width is zero (see alternative code below which checks for width > 0).
Code that does not check for width:
if(com.imebra.ColorTransformsFactory.isMonochrome(dicomImage.getColorSpace());
{
// Retrieve the VOIs (center/width pairs)
com.imebra.vois_t vois = loadedDataSet.getVOIs();
if(!vois.isEmpty())
{
// Get the first VOI setting from the dataset
chain.addTransform(new VOILUT(vois.get(0)));
}
else
{
// The dataset does not have any VOI setting, find the optimal one
com.imebra.VOIDescription voiDescription = VOILUT.getOptimalVOI(dataSetImage, 0, 0, width, height);
chain.addTransform(new VOILUT(voiDescription));
}
}
Alternative code that checks if width is 0:
if(com.imebra.ColorTransformsFactory.isMonochrome(dicomImage.getColorSpace());
{
// Retrieve the VOIs (center/width pairs)
com.imebra.vois_t vois = loadedDataSet.getVOIs();
if(!vois.isEmpty() && vois.get(0).getWidth() > 0.1)
{
// Get the first VOI setting from the dataset
chain.addTransform(new VOILUT(vois.get(0)));
}
else
{
// The dataset does not have any VOI setting, find the optimal one
com.imebra.VOIDescription voiDescription = VOILUT.getOptimalVOI(dataSetImage, 0, 0, width, height);
chain.addTransform(new VOILUT(voiDescription));
}
}

How to generate PDF having HTML like capabilities using Java

I need to create PDF using some java API which has the capabilities like HTML. Basically, I want to create a form which can take input from users, perform some basic client side validations and also generate this PDF programmatically using Java. I am also looking for rich HTML like features like expand, collapse, hyperlinks, add a section on a button click etc. So basically I am trying to create an HTML like page but within PDF.
I have tried using itext but able to only to do only handful of things and not able to add dynamism into PDF. Is there any tool/API which supports this?
PDF itself allows you to embed (a subset of) javascript.
This embedded code can be linked to document events (e.g. opening the document) or specific form elements (e.g. clicking a button, changing the text in a text-input field).
This is a page from their website entitled 'Making a PDF interactive' which focusses on adding form elements.
The book (iText in Action) by Bruno Lowagie (original founder of iText) also goes in great detail. It even shows how to program a calculator in a PDF document, page 232.
I'm just going to copy-paste the relevant section here.
Listing 7.29 Calculator
public void addTextField(PdfWriter writer, Rectangle rect, String name) {
PdfFormField field = PdfFormField.createTextField(writer, false, false, 0);
field.setFieldName(name);
field.setWidget(rect, PdfAnnotation.HIGHLIGHT_NONE);
field.setQuadding(PdfFormField.Q_RIGHT);
field.setFieldFlags(PdfFormField.FF_READ_ONLY);
writer.addAnnotation(field);
}
public void addPushButton(PdfWriter writer, Rectangle rect, String btn, String script) {
float w = rect.getWidth();
float h = rect.getHeight();
PdfFormField pushbutton = PdfFormField.createPushButton(writer);
pushbutton.setFieldName("btn_" + btn);
pushbutton.setWidget(rect, PdfAnnotation.HIGHLIGHT_PUSH);
PdfContentByte cb = writer.getDirectContent();
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, createAppearance(cb, btn, BaseColor.GRAY, w, h));
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_ROLLOVER, createAppearance(cb, btn, BaseColor.RED, w, h));
pushbutton.setAppearance(PdfAnnotation.APPEARANCE_DOWN, createAppearance(cb, btn, BaseColor.BLUE, w, h));
pushbutton.setAdditionalActions(PdfName.U, PdfAction.javaScript(script, writer));
pushbutton.setAdditionalActions(PdfName.E, PdfAction.javaScript( "this.showMove('" + btn + "');", writer));
pushbutton.setAdditionalActions(PdfName.X, PdfAction.javaScript( "this.showMove(' ');", writer));
writer.addAnnotation(pushbutton);
}
public PdfAppearance createAppearance(PdfContentByte cb, String btn, BaseColor color, float w, float h) {
PdfAppearance app = cb.createAppearance(w, h);
app.setColorFill(color);
app.rectangle(2, 2, w - 4, h - 4);
app.fill();
app.beginText();
app.setColorFill(BaseColor.BLACK);
app.setFontAndSize(bf, h / 2);
app.showTextAligned(Element.ALIGN_CENTER, btn, w / 2, h / 4, 0);
app.endText();
return app;
}

Apache PDFBox Renders Straight Line Crooked in PNG

I have a PDF that when I render it to a png it renders a line crooked, or rather with a step in it. This is the PDF and what it should look like: https://drive.google.com/file/d/1E-zucbreD7pVwWc3Z4MNe_lzsP6D9m49/view
Here is the full PNG rendering using PDFBox 2.0.13 and openjdk version 1.8.0_181:
And here is the specific portion of the PNG that has the step:
Excerpt of the page content stream:
q
1 0 0 1 35.761 450.003 cm
0 i
0.75 w
0 0 m
50.923 0 l
S
Q
q
1 0 0 1 86.139 450 cm
0 i
0.75 w
0 0 m
14.9 0 l
S
Q
("cm" is an affine transform, "m" a moveto, "l" a lineto). One can see that the two lines are slightly different, one at 450.003, the other one at 450.
Here's some code that simulates the error by replicating what PDFBox is doing:
BufferedImage bim = new BufferedImage(612, 792, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = (Graphics2D) bim.getGraphics();
RenderingHints r = new RenderingHints(null);
r.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
r.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
r.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.addRenderingHints(r);
g.translate(0, 792);
g.scale(1, -1);
g.setStroke(new BasicStroke(0.75f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 10));
g.setColor(Color.black);
GeneralPath path = new GeneralPath();
path.moveTo(35.761f, 450.003f);
path.lineTo(35.761f + 50.923f, 450.003f);
g.draw(path);
path = new GeneralPath();
path.moveTo(86.139f, 450f);
path.lineTo(86.139f + 14.9f, 450f);
g.draw(path);
g.dispose();
ImageIO.write(bim, "png", new File("...."));
One can get rid of the error by commenting this line:
r.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
This could be done in the source code of PDFBox, or by passing the renderingHints in PDFRenderer.setRenderingHints(). However that one isn't available now, but will be available in 2.0.14 (see issue PDFBOX-4435, try a snapshot). And you can expect the rendering to be of poor quality by not having anti aliasing.
Update:
instead of removing the line mentioned above, add this one:
r.put(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE);
Source.

How can I optimize the weight of SVG images generated using Apache Batik?

I have written the following code using Apache Batik in order to generate SVG images (full source available on GitHub) representing badges showing if code passes a SonarQube quality gate or not:
private SVGGraphics2D generateFor(final QualityGateStatus status) {
// new SVG graphics
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(this.svgGeneratorContext, false);
// set SVG canvas size
svgGraphics2D.setSVGCanvasSize(new Dimension(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT));
// set font
svgGraphics2D.setFont(FONT_FAMILY);
// draw Label background
svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw Label text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
// draw Label text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
// draw result background
svgGraphics2D.setColor(status.displayBackgroundColor());
svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw result text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
// draw result text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
return svgGraphics2D;
}
Examples of produced images (depending on parameters passed to the generateFor() method) can be seen on GitHub as well.
This code works fine, however currently the weight of a generated image is more or less 2KB, which is almost 3 times heavier than travis-ci badges that weight more or less 700 Bytes. Therefor I have the feeling that generated images could be simplified, sized down to ~1KB.
How can I achieve this using Apache Batik ? Should I use another library or use another approach ?
Thanks in advance for your insight and recommendations !
I tried generating your SVG using JFreeSVG 3.0, and with the GZIP option it creates files around 570 bytes. In plain text the SVG is around 1.7 kbytes.
Here is the test program I ran (based on your code, you'll also need your QualityGateStatus class to run this):
/*
* qualinsight-plugins-sonarqube-status
* Copyright (c) 2015, QualInsight
* http://www.qualinsight.com/
*
* This program is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, you can retrieve a copy
* from <http://www.gnu.org/licenses/>.
*/
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.io.IOException;
import org.jfree.graphics2d.svg.SVGGraphics2D;
import org.jfree.graphics2d.svg.SVGUtils;
public final class SVGImageGenerator {
private static final int FONT_SIZE = 11;
private static final Font FONT_FAMILY = new Font("Verdana", Font.PLAIN, FONT_SIZE);
private static final int X_MARGIN = 4;
private static final int CANVAS_HEIGHT = 20;
private static final int LABEL_WIDTH = 46;
private static final String LABEL_TEXT = "quality";
private static final int BACKGROUND_CORNER_ARC_DIAMETER = 6;
private static final Color COLOR_BACKGROUND_LABEL = new Color(85, 85, 85, 255);
private static final Color COLOR_SHADOW = new Color(0, 0, 0, 85);
private static final Color COLOR_TEXT = new Color(255, 255, 255, 255);
private static final int Y_OFFSET_SHADOW = 14;
private static final int Y_OFFSET_TEXT = 14;
private static String generateFor(final QualityGateStatus status) {
// new SVG graphics
final SVGGraphics2D svgGraphics2D = new SVGGraphics2D(LABEL_WIDTH + status.displayWidth(), CANVAS_HEIGHT);
// set font
svgGraphics2D.setFont(FONT_FAMILY);
// draw Label background
svgGraphics2D.setColor(COLOR_BACKGROUND_LABEL);
svgGraphics2D.fillRoundRect(0, 0, LABEL_WIDTH, CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH - BACKGROUND_CORNER_ARC_DIAMETER, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw Label text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_SHADOW);
// draw Label text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(LABEL_TEXT, X_MARGIN, Y_OFFSET_TEXT);
// draw result background
svgGraphics2D.setColor(status.displayBackgroundColor());
svgGraphics2D.fillRoundRect(LABEL_WIDTH, 0, status.displayWidth(), CANVAS_HEIGHT, BACKGROUND_CORNER_ARC_DIAMETER, BACKGROUND_CORNER_ARC_DIAMETER);
svgGraphics2D.fillRect(LABEL_WIDTH, 0, BACKGROUND_CORNER_ARC_DIAMETER, CANVAS_HEIGHT);
// draw result text shadow
svgGraphics2D.setColor(COLOR_SHADOW);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 15);
// draw result text
svgGraphics2D.setColor(COLOR_TEXT);
svgGraphics2D.drawString(status.displayText(), LABEL_WIDTH + X_MARGIN, 14);
return svgGraphics2D.getSVGElement();
}
public static void main(String[] args) {
try {
SVGUtils.writeToSVG(new File("error.svgz"), generateFor(QualityGateStatus.ERROR), true);
SVGUtils.writeToSVG(new File("none.svgz"), generateFor(QualityGateStatus.NONE), true);
SVGUtils.writeToSVG(new File("ok.svgz"), generateFor(QualityGateStatus.OK), true);
SVGUtils.writeToSVG(new File("server_error.svgz"), generateFor(QualityGateStatus.SERVER_ERROR), true);
SVGUtils.writeToSVG(new File("warn.svgz"), generateFor(QualityGateStatus.WARN), true);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

Load image of size greater than 2880 in Bitmap

I am using bitmap to load the image, if the image size is more than 2880 h/w I am getting an error.
BitmapData src = new BitmapData(canvasToPrint.width,canvasToPrint.height)(canvasToPrint.width, canvasToPrint.height);
src.draw(_designArea); // -- encode the jpg
var quality:int = 115;
var jpg:JPEGEncoder = new JPEGEncoder(quality);
var byteArray:ByteArray = jpg.encode(src);
if canvasToPrint.width > 2880 or canvasToPrint.width > 2880 I will get below error at line 1
Error : invalid Bitmap
To over come the above issue I have used bitmapdataunlimited class, as mentioned in the below link.
http://blog.formatlos.de/2008/05/28/bitmapdataunlimited/comment-page-2/#comment-4870
But it only works fine for 4096 pixel of height and width, guide me if any alternative solution is there to create huge bitmap.
If makes a difference which FlashPlayer you are targetting:
versions VS maximum bitmapsize
flashplayer -9 : 2880x2880 px
flashplayer 10 : 4096x4096 px
flashplayer 11 : unlimited
http://www.bit-101.com/blog/?p=2067
Try this

Categories