How to display clear images - codenameone

when displaying images in codename 1 form they appear blurred, where is and how can I solve the problem?
Have tried displaying them using image viewer and ImageLabel but non seems to work for me
Image image = URLImage.createToStorage(
EncodedImage.createFromImage(Utils.getTheme().getImage("default_avatar.png"), false), imageName,
imageUrl);
ImageViewer imageViewer = new ImageViewer(image);
imageViewer.getAllStyles().setBgTransparency(0);

When you download an image with URLImage it's scaled to to the size of default_avatar.png. That would mean you lost all the pixels of a larger size. You should use something like:
ConnectionRequest cr = new ConnectionRequest();
cr.downloadImageToStorage(imageName, img -> {
// callback when image downloaded
});

Related

Codename One: show a (secret) Image from an URL without storing and without caching

I have the following code:
EncodedImage placeholder = EncodedImage.createFromImage(FontImage.createImage(size, size, ColorUtil.GRAY), true);
String url = "...";
Date date = new Date();
URLImage qrCode = URLImage.createToStorage(placeholder, date.getTime() + ".png", url, URLImage.RESIZE_SCALE);
qrCode.fetch();
qrCodeLabel.setIcon(qrCode);
The qrCode image contains a secret that should not be saved on the Storage / FileSystem and that should not be cached in any way. It should be shown to the user only one time.
Because these requirements, of course my code doesn't work as I need, because the image is saved and cached. I prefer that the execution of the code stops until the image is downloaded, instead this code firstly shows the placeholder, then shows the image.
So, my question is which code can I use to show an image in a Label, downloading it from an url with these requirements:
no cache;
no storing;
block of the execution until the image is ready (I have a loading Dialog that I want to dispose when the image is ready).
URLImage was designed for caching. You can obviously delete the storage file but it goes a bit against the core purpose of the class.
Just use something like:
ConnectionRequest q = new ConnectionRequest(imageUrl, false) {
public void postResponse() {
EncodedImage qr = EncodedImage.create(getResponseData());
labelForQr.setIcon(qr);
parentForm.revalidate();
}
};
addToQueue(q);

How to Save Captured Signature on SignaturePad Component to a SQL Server Database

As stated, how should we properly save a captured signature using SignaturePad component to a SQL Server database? Well it should be easy, considering the GetImage(false) method used by the component returns a Bitmap and later on compress the Bitmap into a JPEG and into an array byte using a Stream. The byte array should then be saved directly to the SQL Server database; however, the problem with this approach is that when you retrieve the image from the database, the image is all black. It's like the strokes were never captured, funny thing though is that the background color of the SignaturePad was set to White and the stroke color is Black.
Xamarin.Android Button OnClick Event Handler
var signature = this.SignatureView.GetImage(false); // This returns the Bitmap from SignaturePad.
var imageData = this.ImageToByteArray(signature); // This converts the Bitmap to byte[].
var result = this.SaveDataAsync(imageData); // Save the byte[] to the database.
Xamarin.Android Extension Method
private byte[] ImageToByteArray(Bitmap image)
{
if (image == null) return null;
byte[] result;
using (var stream = new MemoryStream())
{
image.Compress(CompressFormat.Jpeg, 100, stream);
result = stream.ToArray();
stream.Flush();
}
return result;
}
This is the same approach we did on iOS, but doesn't work on Android. Any ideas or working solution would be very much appreciated.
Thanks!
Copied from Xamarin Forum - Components Section
Okay, I think I found a solution to this issue, which was described in this thread Signature Pad for Xamarin.Forms. I am not sure why we need to specify the colors within the GetImage() method, while the properties for the Stroke and Background colors has already been specified. Well, I guess the colors needed to draw the image is not the same with that of the properties.

Copying image from page results in black image

I have a program that copies images from a webpage and saves them locally. On certain webpages the saved image is a completely black screen. First I thought it was a problem in the code that didn't take the good picture. So I started investigating. I went manually to those pages and tried to copy the image(right click, copy image) and it still returned a black image. Can someone tell me how can I bypass this from code? Here is the current code, which works fine for most of the pictures
IHTMLDocument2 doc = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLControlRange imgRange = (IHTMLControlRange)((HTMLBody)doc.body).createControlRange();
foreach (IHTMLImgElement img in doc.images)
{
if (img.alt != "my image alt")
continue;
imgRange.add((IHTMLControlElement)img);
imgRange.execCommand("Copy", false, null);
using (Bitmap bmp = (Bitmap)Clipboard.GetDataObject().GetData(DataFormats.Bitmap))
{
if (bmp != null)
{
bmp.Save("testimg.jpg");
}
}
}
That image has a transparent background.
Therefore, every pixel in the image is black, except that most of them are fully transparent.
Since .jpg files do not support transparency, saving it as a .jpg results in a black image.
If you save it as a .png file (which does support transparency), it should work.

Loading image from URL in Windows Phone 7

I use below code to load image from URL in my winodws phone 7 application.
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
It is working fine for me. But image is loading asynchronously and by the time I want to show some kind of busy indicator there and if image does not exist on such URL then I want to show some default image. How can I achieve that?
I think if you are subscribed to the Image.ImageFailed Event you should be able to show to default image in case of a non-existing image.
Conditions in which this event can occur include the following:
File not found.
Invalid (unrecognized or unsupported) file format.
Unknown file format decoding error after upload.
So something like this might work for you:
image1.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(handlerImageFailed);
Uri uri = new Uri("http://dilbert.com/dyn/str_strip/000000000/00000000/0000000/000000/80000/5000/100/85108/85108.strip.print.gif", UriKind.Absolute)
image1.Source = new BitmapImage(uri);
void handlerImageFailed(object sender, ExceptionRoutedEventArgs e)
{
// Show the default image
}

Adding image F#

I have a stack panel to which I want to add some icons dynamically.
If I add a TextBlock to the stack panel, it works perfectly:
// assuming stackPanel is my stack panel
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)
However, my goal is to add an image, but seems like it fails to resolve the image
let getImageSource(imagePath) =
let uri = new Uri(imagePath, UriKind.Relative)
new BitmapImage(uri);
let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")
stackPanel.Children.Add(icon) // this doesnt work
now when I do:
let icon = new Image()
icon.Source <- getImageSource("images/fileIcon/icon.gif")
stackPanel.Children.Add(icon)
let text = new TextBlock()
text.Text <- "Test"
stackPanel.Children.add(text)
I can see there's an empty space between the texts, as if there is an empty image there.
So my guess is there's something wrong with the way I resolve the image path - but I am not sure why.
Thoughts?
Thanks!
In case your gif's Build Action is Resource, then the proper way to address it is /SilverlightApplication1;component/path/to/file.gif. Here SilverlightApplication1 is the name of your silverlight application
In case it's Build Action is Content, then it's proper address is /path/to/file.gif, always with a leading slash when creating a BitmapImage.
Check out Silverlight 2: Demystifying URI references for app resources for more information.
For easier debugging of image loading problems, hook to the BitmapImage.ImageFailed event and see what kind of errors crop up.
One last note, AFAIK Silverlight doesn't support the GIF format. You might use PNG instead.
You can try with the following Uri if yours is WPF App.
let uri = Uri("pack://application:,,,/asm_name;component/images/fileIcon/icon.gif")
asm_name have to be replaced with your actual assembly name.
if your are working on Silverlight application, you need to modify the uri like this. Assuming that the build action of icon.gif is Resource.
let uri = Uri("../images/fileIcon/icon.gif", UriKind.Relative)
Hope this helps.

Resources