I am trying to create a barcode image from a user entered value. The image is
to be updated while the user types. Hence I am using Ajax (onChange event) on
the text field to regenerate the image. However, the image is not getting
refreshed. It appears that the browser is not recognizing that the image has
been updated. Is there a way to append a "timestamp" on the image url just for
this image?
or should I use a different type of resource?
Note: the value entered has not be persisted so I don't believe I can a
ResourceReference. Please correct me if I am wrong.
Thanks
- Doug
BTW...Here is my code that I am using to generate the bar code:
barCodeValueContainer = new WebMarkupContainer("barCodeValueContainer") {
@Override
public void onBeforeRender() {
if (coupon.getObject().getBarCodeType() == null) {
codeContainer.replace(new WebMarkupContainer("barCode"));
} else if (barCodeErrorMessage == null) {
codeContainer.replace(new BarCodeFragment("barCode", this));
} else {
codeContainer.replace(new ErrorFragment("barCode", this,
barCodeErrorMessage));
}
super.onBeforeRender();
}
public boolean isVisible() {
return coupon.getObject().getBarCodeType() != null;
}
};
private class BarCodeFragment extends Fragment {
public BarCodeFragment(String id, MarkupContainer markupProvider) {
super(id, "barCodeFragment", markupProvider);
setRenderBodyOnly(true);
// Create the barcode bean
String barCodeType = coupon.getObject().getBarCodeType();
String barCodeValue = coupon.getObject().getBarCodeValue();
AbstractBarcodeBean bean = null;
if (barCodeType.equals(BarCodeType.CODE_39.id())) {
bean = new Code39Bean();
// bar width exactly one pixel
((Code39Bean) bean).setWideFactor(3);
} else if (barCodeType.equals(BarCodeType.CODE_128.id())) {
bean = new Code128Bean();
} else if (barCodeType.equals(BarCodeType.UPC_A.id())) {
bean = new UPCABean();
}
if (bean == null || barCodeValue == null) {
add(new ErrorFragment("barCode", CouponDetailEditPanel.this,
"Bar code value not set"));
} else {
final int dpi = 150;
// Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.0f / dpi)); // makes the
// narrow
bean.doQuietZone(false);
// Open output file
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
// Set up the canvas provider for monochrome PNG output
BitmapCanvasProvider canvas = new BitmapCanvasProvider(out,
"image/png", dpi, BufferedImage.TYPE_BYTE_BINARY,
false, 0);
// Generate the barcode
bean.generateBarcode(canvas, barCodeValue);
System.out.println("VALUE: " + barCodeValue);
// Signal end of generation
canvas.finish();
out.close();
final byte[] content = out.toByteArray();
Image img = new Image("barCode", new BarCodeImageResource(
content));
add(img);
} catch (Exception e) {
add(new ErrorFragment("barCode",
CouponDetailEditPanel.this, e.getMessage()));
}
}
}
}
private class BarCodeImageResource extends DynamicImageResource {
private byte[] content;
public BarCodeImageResource(byte[] content) {
super("image/png");
this.content = content;
this.setLastModifiedTime(Time.now());
}
@Override
protected byte[] getImageData() {
return content;
}
}