Big image with many icon or one image one icon? - java-me

I have to display around 100 icons (each 50x50) in Button. I am downloading big png Image which contains all 100 icons, then I create each icon using Image.subImage() method.
But my application getting OutOfMemoryError.
I am thinking about 2 solution for this:
download 100 icons as tar(combined into single) file. So i can
create icon one by one. Big Image need not to be in memory till I
create last icon.
Download big Image but don't create small icon.Then override Button
class to paint image (icon) from big Image.
Which is the best solution? or do you have any other solution for this problem.

LWUIT is designed for small devices, and so should you design your code. So a big image is not a good idea.
You should really use seperate images. and only keep those in memory that you can see. Or you will keep running into outofmemory errors.
I would handle it like this.
Get a cachemap.
if you want an image, check if it isn't already in the cachemap.
if it is, use the image from the cachemap
if it isn't download it and put the image in the cachemap.
when you're out of memory, remove the last image from the cachemap and download the new.
if (imageCache.get(url) != null) {
//#debug
System.out.println("Get cached image from: " + url);
asyncImage.setImage((Image) imageCache.get(url));
asyncImage.setQueued(false);
} else {
//#debug
System.out.println("Start download image from:" + url);
map.put(url, asyncImage);
ImageDownloadService d = new ImageDownloadService(url, new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent n = (NetworkEvent) evt;
Image image = (Image) n.getMetaData();
String url = n.getConnectionRequest().getUrl();
AsyncImage asyncImage = (AsyncImage) ImageManager.this.map.get(url);
map.put(url, asyncImage);
asyncImage.setImage(image);
map.remove(url);
imageCache.put(url, asyncImage.getImage());
asyncImage.setQueued(false);
if (Display.getInstance().getCurrent() instanceof AsyncLoadable) {
((AsyncLoadable) Display.getInstance().getCurrent()).asyncLoaded();
} else {
Display.getInstance().getCurrent().repaint();
}
//#debug
System.out.println("Retrieved image from:" + url);
}
});
d.addResponseCodeListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
NetworkEvent n = (NetworkEvent) evt;
String url = n.getConnectionRequest().getUrl();
AsyncImage asyncImage = (AsyncImage) ImageManager.this.map.get(url);
asyncImage.setQueued(false);
map.remove(n.getConnectionRequest().getUrl());
//#debug
System.out.println("Failed image from:" + url);
}
});
NetworkManager.getInstance().addToQueue(d);

Related

Image downloaded from Azure Storage not being displayed

I'm new to Xamarin. I'm trying display a list of downloaded images. I am downloading images from an APP API on Azure, where I stored the file on Azure Storage.
My server code is the following:
public HttpResponseMessage Get(string PK, string RK)
{
//Creating CloudBlockBlolb...
byte[] bytes = new byte[blockBlob.Properties.Length]
for(int i = 0; i < blockBlob.Properties.Length; i++){
bytes[i] = 0x20;
}
blockBlob.DownloadToByteArray(bytes, 0);
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
resp.Content = new ByteArrayContent(bytes);
resp.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpg");
return resp;
}
My Xamarin code is the following:
public MainPage ()
{
//...
List<PicturePost> list = new List<PicturePost>{
new PicturePost("title", "subtitle", "link/api/Pictures?PK=xxx&RK=yyy")
};
InitializeComponent ();
listView.ItemTemplate = new DataTemplate (typeof(CustomImageCell));
listView.HasUnevenRows = true;
listView.ItemsSource = list;
//...
}
And here is the relevant code for CustomImageCell:
var image = new Image ();
image.SetBinding (Image.SourceProperty, "image");
//...
horizontalLayout.Children.Add (image);
I know that my API call works, because when I test it on the browser, it returns the image. I also know that if I use any random links such as http://www.natureasia.com/common/img/splash/thailand.jpg the image is downloaded and displayed properly. It is only when I use the API link that it doesn't seem to be working. Can someone tell me what I am doing wrong?
so in my public MainPage(), I added the following:
listView.BeginRefresh ();
listView.EndRefresh ();
I realized at some point that the images would take some time to download. I assume that when the listView was created, the images were not finished downloading, so I added the code above... Pretty sure this is not the best way to do this (probably an await would be better, but I don't know where).

sony camera api cardboard application

I want to make an application for cardboard with a sony actioncam. I noticed that we can get the video preview by a SurfaceView method. But I remember that the only way to have a split screen with the camera device is by a SurfaceTexture.
So my question is, is there a way with one actioncam to have 2 video previews simultaneously ( split screen ) ??
thanks
EDIT:
okay so I went ahead and bought an action cam AS200, the sdk sample worked perfectly and i was able to get the video preview very quickly. I tried to duplicate the SimpleStreamSurfaceView with no success as expected. Now I m trying to copy the byte array in order to have two previews in one array. First I have tried to simply create an arraybuffer where i put two times the bitmap array, just to see what changes ... and I was surprised to see that nothing changed ... Here is the code
`
while (mWhileFetching) {
try {
byte[] jpegData = mJpegQueue.take();
ByteBuffer test=ByteBuffer.allocate(jpegData.length *2);
test.put(jpegData);
test.put(jpegData);
frameBitmap = BitmapFactory.decodeByteArray(//
test.array(), 0, test.array().length, factoryOptions);
//frameBitmap.setWidth(frameBitmap.getWidth()*2);
} catch (IllegalArgumentException e) {
if (mInMutableAvailable) {
clearInBitmap(factoryOptions);
}
continue;
} catch (InterruptedException e) {
Log.i(TAG, "Drawer thread is Interrupted.");
break;
}
if (mInMutableAvailable) {
setInBitmap(factoryOptions, frameBitmap);
}
drawFrame(frameBitmap);
}
if (frameBitmap != null) {
frameBitmap.recycle();
}
mWhileFetching = false;
}
};
mDrawerThread.start();
return true;
}
`
Of course I wasnt expecting a great result but why nothing changed ??
I solved it. I just had to draw two times in the canvas to different rectangles.

How to set animated image (gif) in a SWT widget using Tabris Framework

I need to display an animated gif image in the app. In the following code example, I set the setImage property of a Label widget. This works fine in the browser (rap), but mobile app only displays static image without animation. Is there a way to display animated image using Tabris framework?
private void openDialog() {
this.display = getUI().getDisplay();
this.shell = new Shell( this.display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL );
this.shell.setLayout( new GridLayout() );
Image image = createImage( this.display, "resources/loading.gif" );
Label label = new Label( this.shell, SWT.NONE );
label.setImage( image );
this.shell.open();
}
private Image createImage( Display display, String resourceName ) {
ClassLoader classLoader = getClass().getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream( resourceName );
if( inputStream == null ) {
throw new IllegalArgumentException( "Resource not found: " + resourceName );
}
try {
return new Image( display, inputStream );
} finally {
try {
inputStream.close();
} catch( IOException exception ) {
// TODO handle exception
}
}
}
Currently it is not possible to display an animated gif. Tabris will improve support for image display so that feature could be added in the future.

How to display the images in listview

I am working with JavaFX2.0.I need to show the thumbnail images in listview.I am writing the code as below.
ObservableList<BufferedImage> imageList = FXCollections.observableArrayList();
try {
for (int i = 1; i <= pdf.getPageCount(); i++) {
BufferedImage pageImage = pdf.getPageAsImage(i);
imageList.add(pageImage);
} catch (PdfException e) {
_logger.error("Error :" + e.getMessage());
}
thumbnailsList.setItems(imageList);
Here thumbnailsList is the fx:id of the listview.But if i use this code i am getting image object and not an image.Can any one tell me that how can i get the image in listview.
Thank You.
You could use a sample from
http://docs.oracle.com/javafx/2/ui_controls/list-view.htm
which is called "Example 11-4 Creating a Cell Factory".
You have to set custom cell factory. And instead of Rectangle, put an ImageView there.

j2me lwuit list take the same cell background

I am making a list that has persons and every ones rate as a cell background
the list gives every cell the same rate background but when scroll the list up or down the cell that I'm dragging take it's real rate background but other cells stay the same rate background image
I'm using nokia s40 full touch emulator
This is the code that I'm using for the cell renderer
list.setRenderer(new ListCellRenderer() {
public Component getListFocusComponent(List list) {
// TODO Auto-generated method stub
return null;
}
public Component getListCellRendererComponent(List list,
Object value, int index, boolean isSelected) {
String rate = "";
rate = Transportation.getDriverRate(value.toString(),
CityListForm.selectedCity);
rate = (Integer.parseInt(rate)) > 25 ? "" + 5 : ""
+ Integer.parseInt(rate) / 5;
rate = "/" + rate + ".png";
Image image = null;
Label label = null;
try {
image = (Image.createImage(rate));
System.out.println(rate);
} catch (IOException e) {
e.printStackTrace();
}
label = new Label();
label.getStyle().setBgImage(image);
label.setText(value.toString());
label.getStyle().setAlignment(RIGHT);
label.setTextPosition(RIGHT);
Style style = label.getStyle();
style.setBgColor(16777215);
style.setFgColor(0);
label.setPreferredH(42);
return label;
}
});
Use List.setMutableRendererBackgrounds(true);
thnx for the answer
i solved it by making a container and butting two labels inside it one for the value and the other for the rate icon and give the container a layout and return the container in the getListCellRendererComponent() method

Resources