How to create a LWUIT Image object from a MIDP native image? - lwuit

In the documentation of LWUIT there is the createImage method which creates a LWUIT Image object from a MIDP native image ( public static Image createImage(java.lang.Object nativeImage) ).
I want to know how to implement the java.lang.Object parameter in order to set a MIDP native image as the parameter of the method ? For example I capture a photo from the camera ( byte[] rawImage = myVideoControl.getSnapshot(null); ) and I create the image file in the filesystem :
myFileConnection = (FileConnection) Connector.open("file:///"+pRoot+photoDirectory+"/"+photoName);
myFileConnection.openOutputStream().write(rawImage);
. So how to pass the Object parameter to the createImage method ?

Image img = Image.createImage(myFileConnection.openInputStream());
EDIT:
see
javax.microedition.lcdui.Image img = javax.microedition.lcdui.Image.createImage("/images/card.png");
com.sun.lwuit.Image ii = com.sun.lwuit.Image.createImage(img);

Related

How to get the path and file size of some Unity built-in assets?

Background
I am developing a Unity editor plugin that enables users to send a selected image file to a REST API endpoint in the cloud for processing (e.g. adding transforms and optimizations). The plugin also shows a comparison of the selected image's details before and after processing (e.g. width/height/size before vs after).
The user selects the desired image through the following piece of code:
selected_texture = (Texture2D) EditorGUI.ObjectField(drawing_rect, selected_texture, typeof(Texture2D), false);
Once its selected, I can then get the respective file size by doing this:
file_size = new FileInfo(AssetDatabase.GetAssetPath(selected_texture)).Length;
Problem
This works for most textures selected, but I encounter an error when I choose a built-in Unity texture. Any guidance would be greatly appreciated.
FileNotFoundException: Could not find file 'Resources/unity_builtin_extra'
There are two built-in asset-librarys in Unity:
BuiltIn-Library in "Resources/unity_builtin_extra": contains UGUI sprite、Default-Material、Shader and so on.
BuiltIn-Library in "Library/unity default resources": contains built-in 3D mesh and OnGUI assets.
If you are using AssetDatabase.GetAssetPath, you will always get one or another path above.
To solve the problem, you need do something like below code:
public const string BuiltinResources = "Resources/unity_builtin_extra";
public const string BuiltinExtraResources = "Library/unity default resources";
public static bool IsBuiltInAsset(string assetPath)
{
return assetPath.Equals(BuiltinResources) || assetPath.Equals(BuiltinExtraResources);
}
public static long GetTextureFileLength(Texture texture)
{
string texturePath = AssetDatabase.GetAssetPath(texture);
if (IsBuiltInAsset(texturePath))
{
/*
* You can get all built-in assets by this way.
*
var allAssets = AssetDatabase.LoadAllAssetsAtPath(BuiltinResources);
var allExtraAssets = AssetDatabase.LoadAllAssetsAtPath(BuiltinExtraResources);
*/
// not supportted
// return -1;
// using MemorySize
return Profiler.GetRuntimeMemorySizeLong(texture);
}
else
{
return new FileInfo(texturePath).Length;
}
}

How to insert image in mupdf library

I am using mupdf to sign a pdf.
And I succeed to sign a annotation in pdf with function "pdf_update_ink_appearance"
Now I'm trying to insert an image into pdf.
I add below codes to insert image:
image = fz_new_image_from_file(ctx, "/storage/emulated/0/a.jpg");
fz_fill_image(ctx, dev, image, &page_ctm, 1.0f);
And the image doesn't show up in pdf.
I try another method, but the image also can't show up in pdf.
How to add a transparent image to PDF with mupdf using SMask?
Can anyone help this situation?
Thanks.
I am one of PyMuPDF's authors (a Python binding for MuPDF) and solved this exact task. You might have a look at the source code in GitHub.
The basic process is:
Create an image and then a pixmap from the file. If this pixmap has alpha (i.e. transparency), create another pixmap containg the alpha bytes and link like follows. After this add the main pixmap to the PDF. Finally Add an XObject referencing it to the page's /Resources, and invoke the XObject with a "Do" operator in the page's /Contents object.
if (filename)
{
image = fz_new_image_from_file(gctx, filename);
pix = fz_get_pixmap_from_image(gctx, image, NULL, NULL, 0, 0);
if (pix->alpha == 1)
{
j = pix->n - 1;
pm = fz_new_pixmap(gctx, NULL, pix->w, pix->h, seps, 0);
s = pix->samples;
t = pm->samples;
for (i = 0; i < pix->w * pix->h; i++)
t[i] = s[j + i * pix->n];
mask = fz_new_image_from_pixmap(gctx, pm, NULL);
zimg = fz_new_image_from_pixmap(gctx, pix, mask);
fz_drop_image(gctx, image);
image = zimg;
zimg = NULL;
}
}
Do have a look at file fitz.i and search for function insertImage. It's an SWIG interface file, but that part is plain C interfacing with MuPDF.

Eclipse RCP - Calling image from AbstractUIPlugin.getImageDescriptor and ImageRegistry

What is the different between calling image using AbstractUIPlugin.getImageDescriptor and putting an image into the image registry and calling from the image registry?
Seems like both are doing the same job. The first one is easier (less line of code!).
This code:
ImageDescriptor id = Activator.getImageDescriptor("image/image.png");
Image image = id.createImage();
vs
Putting into an image registry first..
protected void initializeImageRegistry(ImageRegistry registry) {
super.initializeImageRegistry(registry);
Bundle bundle = Platform.getBundle(ID);
ImageDescriptor myImage = ImageDescriptor.createFromURL(
FileLocator.find(bundle,
new Path("image/image.png"),
null));
registry.put(MY_IMAGE_ID, myImage);
}
then calling image from image registry..
AbstractUIPlugin plugin = Activator.getDefault();
ImageRegistry imageRegistry = plugin.getImageRegistry();
Image myImage = imageRegistry.get(Activator.MY_IMAGE_ID);
It creates separate images each time you invoke it. Finally you will end up with "No more handles" exception. :)
ImageDescriptor id = Activator.getImageDescriptor("image/image.png");
Image image = id.createImage();
Always cache images, reuse and dispose when you close your app. If you use ImageRegistry, AbstractUIPlugin will do the job of disposing it for you.

Gwt Image original size

I use an Image object to load a png image as a thumbnail by calling its setPixelSize() method to resize the image. I also need to retrieve the original size of the image as integers at some point. How can I get the original sizes (width, height) of the image?
ok i found a workaround: I use a dummy container (SimplePanel) and load the image without scaling save its real dimensions and then remove the container from the parent and discard the new Image object. I don't know if this a good workaround, but it works. Although i would like to know if there is another way...
problem of the workaround: I have a droplist from which i can select logical folders (which contain images). When i select a new folder, and the new set of images is loaded on display, then i get 0 for width and 0 for height.
private void getTrueSize(String fullUrl) {
Image trueImage = new Image();
this.tstCon.add(trueImage);
trueImage.setUrl(fullUrl);
this.trueHeight = trueImage.getHeight();
this.trueWidth = trueImage.getWidth();
//this.tstCon.remove(trueImage);
//trueImage = null;
GWT.log("Image [" + this.imgTitle + "] -> height=" + this.trueHeight + " -> width=" + this.trueWidth);//
}
Extend the Image class and implement onAttach() method. This method is called when a widget is attached to the browser's document.
public class Thumb extends Image {
public Thumb(){
super();
}
protected void onAttach(){
Window.alert(getOffsetHeight()+" "+getOffsetWidth()); //this should give you the original value
setPixelSize(10,10); // this should be the visible value
super.onAttach();
}
}
if this doesn't work, try implementing onLoad() instead of onAttach(), since onLoad() will be called after the image is added to DOM so that it definately should work.
Its the easiest way to create a new hidden Image (placed absolute outside the viewport) and read the size. There is a JavaScript lib that can read the exif data of the image but this would be overkill in this case.
Read naturalHeight and naturalWidth of the image element after image load.
public Panel() {
image = new Image();
image.addLoadHandler(this::onLoad);
}
private void onLoad(#SuppressWarnings("unused") LoadEvent event) {
origImageWidth = getNaturalWidth(image);
origImageHeight = getNaturalHeight(image);
}
private static int getNaturalHeight(Image img) {
return img.getElement().getPropertyInt("naturalHeight"); //$NON-NLS-1$
}
private static int getNaturalWidth(Image img) {
return img.getElement().getPropertyInt("naturalWidth"); //$NON-NLS-1$
}

using GWTCanvas with ImageResource

I want to use the clientBundle capability of GWT to load only 1 image, which is composed of many sprites, with GWTCanvas. My initial take was to just convert the ImageResource into an ImageElement, but apparently that doesn't seem to work:
public interface Bundle implements ClientBundle{
public static Bundle INSTANCE = GWT.create(Bundle .class);
#Source("/img/tile1.png")
public ImageResource tile1()
}
final GWTCanvas canvas = new GWTCanvas(400,400);
canvas.drawImage(ImageElement.as(new Image(Bundle.INSTANCE.tile1()).getElement()), 0, 0);
i tried adding the Image to RootPanel first (to force a load), but that doesn't seem to work too. Perhaps the timings are incorrect. Does anyone have a clue as to how I can draw an imageResource using GWTCanvas?
This works: (GWT 2.4)
// load:
SafeUri uri= imageResource.getSafeUri();
ImageElement imgElement= ImageElement.as((new Image(uri)).getElement());
// render
context.drawImage(imgElement, 0, 0);
You can get the image from a bundle using a data URI, but you'll need to manage the asynchrony as you would with a remote image.
final Image image = new Image(resources.imageResource().getURL());
RootPanel.get().add(image);
image.setVisible(false);
fetchedImage.addLoadHandler(new LoadHandler() {
public void onLoad(LoadEvent event) {
context.drawImage(ImageElement.as(image.getElement()), 0, 0);
}
});
Using ClientBundled image in the way you want isn't possible. Images combined to one big image are displayed as background images which is based on the feature of the browser to show only part of an image. GWT calls this 'clipped' mode. So when you try to get the element of that image, the actual src tag is empty as the image is a background image. If you want to display images on the Canvas it must be an actual link to an image.
You might try using the ImageResource's getURL() method when you create the image:
canvas.drawImage(ImageElement.as(new Image(Bundle.INSTANCE.tile1().getUrl()).getElement(), 0, 0);
I was having the same problem when using a ClientBundle with GWT 2.2.0's Canvas type and this fixed it for me.
They are correct, just use getSafeUri() instead of getURL()
The solution of Tom Fishman didn't work for me, because the images weren't loaded at the time when i called canvas.drawImage(). This solution works (also for big images):
// Create Image
SafeUri uri = resource.getSafeUri();
Utils.console("URI: "+ uri);
final Image img = new Image(uri);
// Add the image to the RootPanel to force the image to be loaded.
RootPanel.get().add(img);
// The image has to be added to the canvas after the image has been loaded.
// Otherwise the bounding box of the image is 0,0,0,0 and nothing will be drawn.
img.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
// Create the image element.
ImageElement imgElement = ImageElement.as(img.getElement());
// Render the image on the canvas.
context2d.drawImage(imgElement, offsetX, offsetY);
// Delete the image from the root panel.
RootPanel.get().remove(img);
}
});