use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.
the class HTMLComponent method handleImage.
/**
* Handles the IMG tag. This includes calculating its size (if available), applying any links/accesskeys and adding it to the download queue
*
* @param imgElement the IMG element
* @param align th current alignment
* @param cmd The submit command of a form, used only for INPUT type="image"
*/
private void handleImage(HTMLElement imgElement, int align, Command cmd) {
String imageUrl = imgElement.getAttributeById(HTMLElement.ATTR_SRC);
Label imgLabel = null;
if (imageUrl != null) {
String alignStr = imgElement.getAttributeById(HTMLElement.ATTR_ALIGN);
// Image width and height
int iWidth = calcSize(getWidth(), imgElement.getAttributeById(HTMLElement.ATTR_WIDTH), 0, false);
int iHeight = calcSize(getHeight(), imgElement.getAttributeById(HTMLElement.ATTR_HEIGHT), 0, false);
// Whitespace on the image sides (i.e. Margins)
int hspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_HSPACE));
int vspace = getInt(imgElement.getAttributeById(HTMLElement.ATTR_VSPACE));
int totalWidth = iWidth + hspace * 2;
if ((FIXED_WIDTH) && (x + totalWidth >= width)) {
newLine(align);
}
// Alternative image text, shown until image is loaded.
String altText = imgElement.getAttributeById(HTMLElement.ATTR_ALT);
String imageMap = imgElement.getAttributeById(HTMLElement.ATTR_USEMAP);
if (link != null) {
// This image is inside an A tag with HREF attribute
imgLabel = new HTMLLink(altText, link, this, mainLink, false);
if (mainLink == null) {
mainLink = (HTMLLink) imgLabel;
}
if (accesskey != '\0') {
// accessKeys.put(new Integer(accesskey), imgLabel);
addAccessKey(accesskey, imgLabel, false);
}
if (!PROCESS_HTML_MP1_ONLY) {
((HTMLLink) imgLabel).isMap = (imgElement.getAttributeById(HTMLElement.ATTR_ISMAP) != null);
}
} else if (cmd != null) {
// Special case of an image submit button
imgLabel = new Button(cmd);
if ((altText != null) && (!altText.equals(""))) {
imgLabel.setText(altText);
}
if (firstFocusable == null) {
firstFocusable = imgLabel;
}
} else if (imageMap != null) {
// Image Map
imgLabel = new HTMLImageMap(this);
if (imageMapComponents == null) {
imageMapComponents = new Hashtable();
}
if (imageMap.startsWith("#")) {
// Image map are denoted by # and then the map name (But we also tolerate if map is specified without #)
imageMap = imageMap.substring(1);
}
imageMapComponents.put(imageMap, imgLabel);
if ((imageMapData != null) && (imageMapData.containsKey(imageMap))) {
ImageMapData data = (ImageMapData) imageMapData.get(imageMap);
((HTMLImageMap) imgLabel).mapData = data;
}
} else {
imgLabel = new Label(altText);
}
if ((iWidth != 0) || (iHeight != 0)) {
// reserve space while loading image if either width or height are specified, otherwise we don't know how much to reserve
iWidth += imgLabel.getStyle().getPadding(Component.LEFT) + imgLabel.getStyle().getPadding(Component.RIGHT);
iHeight += imgLabel.getStyle().getPadding(Component.TOP) + imgLabel.getStyle().getPadding(Component.BOTTOM);
imgLabel.setPreferredSize(new Dimension(iWidth, iHeight));
} else {
// If no space is reserved, make a minimal text, otherwise Codename One won't calculate the size right after the image loads
if ((imgLabel.getText() == null) || (imgLabel.getText().equals(""))) {
imgLabel.setText(" ");
}
}
// It is important that the padding of the image component itself will be all 0
// This is because when the image is loaded, its preferred size is checked to see if its width/height were preset by the width/height attribute
imgLabel.getSelectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getUnselectedStyle().setPadding(0, 0, 0, 0);
imgLabel.getSelectedStyle().setFont(font.getFont());
imgLabel.getUnselectedStyle().setFont(font.getFont());
int borderSize = getInt(imgElement.getAttributeById(HTMLElement.ATTR_BORDER));
if (borderSize != 0) {
imgLabel.putClientProperty(CLIENT_PROPERTY_IMG_BORDER, new Integer(borderSize));
} else {
borderSize = 1;
}
imgLabel.getUnselectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getSelectedStyle().setBorder(Border.createLineBorder(borderSize));
imgLabel.getUnselectedStyle().setBgTransparency(0);
imgLabel.getSelectedStyle().setBgTransparency(0);
Container imgCont = new Container(new BorderLayout());
imgCont.addComponent(BorderLayout.CENTER, imgLabel);
imgCont.getSelectedStyle().setMargin(vspace, vspace, hspace, hspace);
imgCont.getUnselectedStyle().setMargin(vspace, vspace, hspace, hspace);
curLine.addComponent(imgCont);
x += totalWidth;
// Alignment
imgLabel.setAlignment(getHorizAlign(alignStr, align, false));
imgLabel.setVerticalAlignment(getVertAlign(alignStr, Component.CENTER));
if (showImages) {
if (docInfo != null) {
imageUrl = docInfo.convertURL(imageUrl);
threadQueue.add(imgLabel, imageUrl);
} else {
if (DocumentInfo.isAbsoluteURL(imageUrl)) {
threadQueue.add(imgLabel, imageUrl);
} else {
if (htmlCallback != null) {
htmlCallback.parsingError(HTMLCallback.ERROR_NO_BASE_URL, imgElement.getTagName(), imgElement.getAttributeName(new Integer(HTMLElement.ATTR_SRC)), imageUrl, "Ignoring Image file referred in an IMG tag (" + imageUrl + "), since page was set by setBody/setHTML/setDOM so there's no way to access relative URLs");
}
}
}
}
if (loadCSS) {
imgElement.setAssociatedComponents(imgCont);
}
}
}
use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.
the class IndexedImage method scaled.
/**
* {@inheritDoc}
*/
public Image scaled(int width, int height) {
int srcWidth = getWidth();
int srcHeight = getHeight();
// no need to scale
if (srcWidth == width && srcHeight == height) {
return this;
}
Dimension d = new Dimension(width, height);
Image i = getCachedImage(d);
// currently we only support byte data...
i = new IndexedImage(width, height, palette, scaleArray(imageDataByte, width, height));
cacheImage(d, i);
return i;
}
use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.
the class AndroidImplementation method onActivityResult.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == ZOOZ_PAYMENT) {
((IntentResultListener) pur).onActivityResult(requestCode, resultCode, intent);
return;
}
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAPTURE_IMAGE) {
try {
String imageUri = (String) Storage.getInstance().readObject("imageUri");
Vector pathandId = StringUtil.tokenizeString(imageUri, ";");
String path = (String) pathandId.get(0);
String lastId = (String) pathandId.get(1);
Storage.getInstance().deleteStorageFile("imageUri");
clearMediaDB(lastId, path);
callback.fireActionEvent(new ActionEvent(path));
return;
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == CAPTURE_VIDEO) {
String path = (String) Storage.getInstance().readObject("videoUri");
Storage.getInstance().deleteStorageFile("videoUri");
callback.fireActionEvent(new ActionEvent(path));
return;
} else if (requestCode == CAPTURE_AUDIO) {
Uri data = intent.getData();
String path = convertImageUriToFilePath(data, getContext());
callback.fireActionEvent(new ActionEvent(path));
return;
} else if (requestCode == OPEN_GALLERY) {
Uri selectedImage = intent.getData();
String scheme = intent.getScheme();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
// this happens on Android devices, not exactly sure what the use case is
if (cursor == null) {
callback.fireActionEvent(null);
return;
}
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
if (filePath == null && "content".equals(scheme)) {
// locally
try {
InputStream inputStream = getContext().getContentResolver().openInputStream(selectedImage);
if (inputStream != null) {
String name = getContentName(getContext().getContentResolver(), selectedImage);
if (name != null) {
filePath = getAppHomePath() + getFileSystemSeparator() + name;
File f = new File(filePath);
OutputStream tmp = createFileOuputStream(f);
byte[] buffer = new byte[1024];
int read = -1;
while ((read = inputStream.read(buffer)) > -1) {
tmp.write(buffer, 0, read);
}
tmp.close();
inputStream.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
callback.fireActionEvent(new ActionEvent(filePath));
return;
} else {
if (callback != null) {
callback.fireActionEvent(new ActionEvent("ok"));
}
return;
}
}
// clean imageUri
String imageUri = (String) Storage.getInstance().readObject("imageUri");
if (imageUri != null) {
Storage.getInstance().deleteStorageFile("imageUri");
}
if (callback != null) {
callback.fireActionEvent(null);
}
}
use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.
the class Resources method readMultiImage.
Image readMultiImage(DataInputStream input, boolean skipAll) throws IOException {
EncodedImage resultImage = null;
if (dpi == -1) {
dpi = Display.getInstance().getDeviceDensity();
}
int dpiCount = input.readInt();
int bestFitOffset = 0;
int bestFitDPI = 0;
int[] lengths = new int[dpiCount];
int[] dpis = new int[dpiCount];
boolean found = false;
for (int iter = 0; iter < dpiCount; iter++) {
int currentDPI = input.readInt();
lengths[iter] = input.readInt();
dpis[iter] = currentDPI;
if (bestFitDPI != dpi && dpi >= currentDPI && currentDPI >= bestFitDPI) {
bestFitDPI = currentDPI;
bestFitOffset = iter;
found = true;
}
}
if (!found) {
// special case for low DPI devices when running with high resolution resources
// we want to pick the loweset resolution possible
bestFitDPI = dpis[0];
bestFitOffset = 0;
for (int iter = 1; iter < dpiCount; iter++) {
if (dpis[iter] < bestFitDPI) {
bestFitDPI = dpis[iter];
bestFitOffset = iter;
}
}
}
if (runtimeMultiImages && !skipAll) {
byte[][] data = new byte[dpiCount][];
for (int iter = 0; iter < lengths.length; iter++) {
int size = lengths[iter];
data[iter] = new byte[size];
input.readFully(data[iter], 0, size);
}
return EncodedImage.createMulti(dpis, data);
} else {
for (int iter = 0; iter < lengths.length; iter++) {
int size = lengths[iter];
if (!skipAll && bestFitOffset == iter) {
byte[] multiImageData = new byte[size];
input.readFully(multiImageData, 0, size);
resultImage = EncodedImage.create(multiImageData);
} else {
while (size > 0) {
size -= input.skip(size);
}
}
}
}
return resultImage;
}
use of com.codename1.ui.util.xml.Data in project CodenameOne by codenameone.
the class Resources method createImage.
Image createImage(DataInputStream input) throws IOException {
if (majorVersion == 0 && minorVersion == 0) {
byte[] data = new byte[input.readInt()];
input.readFully(data, 0, data.length);
return EncodedImage.create(data);
} else {
int type = input.readByte() & 0xff;
switch(type) {
// PNG file
case 0xf1:
// JPEG File
case 0xf2:
byte[] data = new byte[input.readInt()];
input.readFully(data, 0, data.length);
if (minorVersion > 3) {
int width = input.readInt();
int height = input.readInt();
boolean opaque = input.readBoolean();
return EncodedImage.create(data, width, height, opaque);
}
return EncodedImage.create(data);
// Indexed image
case 0xF3:
return createPackedImage8();
// SVG
case 0xF5:
{
int svgSize = input.readInt();
if (Image.isSVGSupported()) {
byte[] s = new byte[svgSize];
input.readFully(s);
String baseURL = input.readUTF();
boolean animated = input.readBoolean();
loadSVGRatios(input);
byte[] fallback = new byte[input.readInt()];
if (fallback.length > 0) {
input.readFully(fallback, 0, fallback.length);
}
return Image.createSVG(baseURL, animated, s);
} else {
svgSize -= input.skip(svgSize);
while (svgSize > 0) {
svgSize -= input.skip(svgSize);
}
// read the base url, the animated property and screen ratios to skip them as well...
input.readUTF();
input.readBoolean();
input.readFloat();
input.readFloat();
byte[] fallback = new byte[input.readInt()];
input.readFully(fallback, 0, fallback.length);
return EncodedImage.create(fallback);
}
}
// SVG with multi-image
case 0xf7:
{
int svgSize = input.readInt();
if (Image.isSVGSupported()) {
byte[] s = new byte[svgSize];
input.readFully(s);
String baseURL = input.readUTF();
boolean animated = input.readBoolean();
Image img = readMultiImage(input, true);
Image svg = createSVG(animated, s);
if (svg.getSVGDocument() == null) {
return img;
}
return svg;
} else {
svgSize -= input.skip(svgSize);
while (svgSize > 0) {
svgSize -= input.skip(svgSize);
}
String baseURL = input.readUTF();
// read the animated property to skip it as well...
input.readBoolean();
return readMultiImage(input);
}
}
// mutli image
case 0xF6:
return readMultiImage(input);
case 0xEF:
Timeline tl = readTimeline(input);
return tl;
// Fail this is the wrong data type
default:
throw new IOException("Illegal type while creating image: " + Integer.toHexString(type));
}
}
}
Aggregations