Loading correct texture from Atlas with multiple pages - java

I have a big set of images saved in one atlas, but due to the 2048*2048 size limitation of a single page I had to split my packs in 12 pages.
My problem is that when I use atlas.findRegion(imageName).getTexture() to load the texture, it loads the whole 2048*2048 page and not the single texture inside the page (the image pack contains the correct image though, so I know it's loading the correct file, I just don't understand why it's not cropping the texture around the image I pass as parameter to findRegion).
Thanks in advance, hope the question is detailed enough to receive an answer.

The problem was actually calling the getTexture() method, I just needed to load the Region. More info here loading regions of textureatlas in libgdx loads the entire file

Related

Progressive Image Rendering, loading image pixel by pixel & updating imageview in imageLoader

I am working on an application where i need to load the server images in list. But the way of displaying images is bit different. I need to display image as it gets downloaded i.e. pixel by pixel. Initially the image sets as blur image than as it gets downloaded, its gets more sharper and results into the original image. If you want to see what i am talking about than you can refer to the below link:
Progressive Image Rendering
In this you can see the demo of the superior PNG style two-dimensional interlaced rendering(the second one). I goggled a lot but did not find any relevant answer to my query.
Any sort of help would be appreciable.
Thanks in advance.
Try Facebook's Freso project: http://frescolib.org/#streaming
If you use Glide, the library recommended by Google for loading images in Android, you can achieve this kind of behavior with .thumbnail this receives the fraction of the resolution you'll like your image to show while the full resolution finishes loading.
Glide.with( context )
.load( "image url" )
.thumbnail( 0.1f ) //rate of the full resoultion to show while loading
into( imageView ); //ImageView where you want to display the image
Here is a more extensive explanation of the library and the thumbnails:
https://futurestud.io/blog/glide-thumbnails

Is there a way to proactively load photos with Universal Image Loader?

Is there a way to load the images that will come on the screen soon as opposed to loading the images that are currently in view?
If you're asking how to pre-load images so they are freshly cached, so that when the corresponding ImageView comes on screen the loading is nice and fast, you could use (somewhat untested):
String uri = getUriOfImageAboutToComeOnScreen(...);
ImageLoader.getInstance().loadImage(uri, null /* or use a listener */);
Of course, the magic contained in getUriOfImageAboutToComeOnScreen() is up to you :)

is it better to pass an image through classes or recreate the image in each class (android)?

Im creating a simple application that converts the image dimensions to a size that the user wants, im laying it out over a 3 screens
the first screen allows a user to select an image and it is displayed on screen.
the second screen then displays the attributes of the file (path, name, height and width) with the option of adjusting the name, height and width.
the third screen displays the resized image with an option to save the new image.
at the moment im only passing the image url between the classes and decoding the bitmap within each class eg
Bitmap bmap = BitmapFactory.decodeImage(image_URL);
my question is it better to pass the URL between the classes or pass the Bitmap?
many thanks
You have several options:
Share the image through static data - Preferably in your
Application class. Example: Using a class to store static data in Java?
Pass the image through the Intent data which is possible, but
highly discouraged.
Pass the URL as you suggest and re-open and resize it each time.
Use a combination. For example, you should pass only image
information to your second screen which displays said information.
I suggest that the first bullet is best practice and will work well for you.
That should be the correct approach. Passing Bitmap shouldn't be done from one activity to another. Passing the path is always recommended
I think that dependent on where is your image if (you have the image on device ) I think the best is to pass Image path , but if you have the image on web and you have to download it I think passing bitmap is better than pass URL(Just to decries internet connection & faster for the user )

Lazily loading images in Java

Let us consider this code snippet
Image img=Toolkit.getDefaultToolkit().getImage("1.png");
g.drawImage(img,0,0,null);
What the code does is load the image 1.png and draw it on a graphic context. Now what I observe is drawImagedo not draw any image the first time it is called. Instead it draws the image on further calls. Now I think this behavior is due to asynchronous image load or the lazy behavior of the method.
To correct the problem what I could do is use the javax.swing.ImageIcon class like this.
Image img=new ImageIcon("1.png").getImage();
g.drawImage(img,0,0,null);
I want to know what are the other better ways to perform the same task.
It depends.
If you are loading the image over a "slow" connection (like the internet) or you are loading a sizable image, it's best to use a lazy loading approach.
This allows the application to continue running while the image is begin loaded.
Andrew is correct, you should be using g.drawImage(img,0,0,this), as this allows the component to automatically update itself once the image has completed loading, without you needing to perform any other actions.
If you application relies on the image for some part of it's operation OR you are loading small images locally, it would be sufficent to use something like...
BufferedImage image = ImageIO.read(imageResource);
// Where image resource is either a file or local URL (such as an embedded resource)
I personally, tend to use a background thread to load my images in most cases and use ImageIO. In my paint method, I might use a small placeholder image if required to let the user know that I am loading the image.
You will also need to take into consideration the type of image you are loading as well. While ImageIO has a larger support for image formats, loading animated GIFs is problematic and requires significant more work on you part to achieve.
ps - Don't load images in you component paint methods - paint can be called numerous times and loading images (or other resources) will dramatically slow the repaint process, making your application lag...
Try using ImageIO.read() for loading the images. This will block until the image is fully loaded. Check this page.

Fastest way to load and display a jpeg on a SurfaceView?

This is a bit of a followup to my last question: Canvas is drawing too slowly
Now that I can draw images more quickly, the problem I am faced with is that the actual loading of the images takes far too long.
In the app I am working on, the user is able to play back video frames (jpegs) in succession, as though he is viewing the video in realtime. I have been using BitmapFactory.decodeFile() to load each jpeg in a Bitmap. I'm unable to load all images at once since there are about 240 of them, and that would use up all of my heap space. What I have been doing is preloading up to 6 at a time into an array by way of a separate thread in order to cut down on the time it takes for each image to display.
Unfortunately, it takes somewhere between 50 and 90ms to load an image, and I need to show an image every 42ms. Is there a faster way to load images possibly?
For clarification, these images are in a folder on the SD card, and they are all 720x480 jpegs. I am sampling them at half that size to cut down on memory usage.
I ended up doing this quite a bit differently than I had originally envisioned. There was quite a bit to it, but here's the gist of how I achieved my goal:
All images are stored on SD card and written to one file (each image takes up X bytes in the file)
Use native code to read from and write to the image file
When requesting an image, I pass the index of the image in the list and a bitmap object (RGB_565) to the native code using a JNI wrapper
The native code locks the bitmap surface, writes pixel data (as a uint8_t**) directly to the bitmap, then unlocks it
The image is rendered to the screen
By doing it this way, I only needed to store one image in memory at a time, and I was able to bypass garbage collection (since the bitmap was only created once and then repopulated natively). I hope someone else might find this strategy useful.
Guess you already tried all methods in this tutorial http://www.higherpass.com/Android/Tutorials/Working-With-Images-In-Android/2/ and chosen the fastest. Maybe tweaking resizing can decrease loading time.
Best of all would of course be if you didn't have to resize the images at all. If you have full control of the images maybe you could try to pack them as sprites, see article http://www.droidnova.com/2d-sprite-animation-in-android,471.html

Categories