i have an old java applet (not written by me) that needs better peroformance.
I use allot of:
getToolkit().getImage(imageURL)
My problems with this are:
It seams images will loaded one by one and not parallel
The request have all the http keep-alive flag but for each image it opens a new tcp connection. And never close the connections.
Is there a solution to process images parallel with use http keep-alive function?
A short example, hwo the app work:
public class test
{
Map<String, URL> urlList = new HashMap<String, URL>();
urlList.put("test", "/test.jpg");
urlList.put("grrr", "/grrr.jpg");
urlList.put("htutz", "/htutz.jpg");
urlList.put("fsdfgf", "/fsdfgf.jpg");
urlList.put("fdgsfd", "/fdgsfd.jpg");
urlList.put("fdgtzt", "/fdgtzt.jpg");
urlList.put("reterg", "/reterg.jpg");
urlList.put("ssdfsfdgsfd", "/ssdfsfdgsfd.jpg");
for (URL imageUrl: urlList) {
{
new ImageExample(imageUrl);
}
}
public class ImageExample extends JApplet {
Image image;
public void ImageExample(URL imageUrl) {
image = getToolkit().getImage(imageUrl);
}
public void paint(Graphics g) {
g.drawImage(image, 20, 20, this);
}
#Override
public void draw( Graphics g )
{
paint(g);
}
}
Create a new thread to load each image and terminate it when the image is loaded.
Like this you will load all the images simultaneously and not anymore image by image.
(And also you will not block the ui thread)
(Since you will not use the cache)
Related
I'm working on movies Android app which get list of movies from an API which provides a poster path for all movies.
I want to get the image as Bitmap from the image's URL to save it as a Bitmap variable in the model class. I want to save the image as blob in the DB to retrieve it directly without redownloading it each time the user opens the app. Is that possible?
I want to do something like this, but it always returns null.
private Bitmap posterBitmap;
public void setPosterBitmap () {
Picasso.get().load(POSTERS_URL).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
posterBitmap = bitmap; // I don't want to set it to an Image view here
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
});
}
Thanks in advance.
new Thread(() -> {
try {
Bitmap mBitmap = Picasso.get().load(link).get();
} catch (Exception e) {
Log.e(""+e);
}
}).start();
By using
implementation 'com.squareup.picasso:picasso:2.71828'
This code is working for me:
...
private static final String TAG = MainActivity.class.getName();
private Target mTarget;
...
mTarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if (bitmap == null) {
Log.w(TAG, "Null");
} else {
Log.i(TAG, "Worked");
}
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
Log.w(TAG, "failed");
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.i(TAG, "Prepare");
}
};
// Small image loads without resize
// Picasso.get().load("http://www.theretirementmanifesto.com/wp-content/uploads/2016/08/Yoda-free-clip-art-680x410.jpg").into(mTarget);
// Mega high res out of memory image
Picasso.get().load("https://upload.wikimedia.org/wikipedia/commons" +
"/5/5e/M104_ngc4594_sombrero_galaxy_hi-res.jpg").
resize(100, 100).into(mTarget);
Also I'm assuming that the following line is in the manifest:
<uses-permission android:name="android.permission.INTERNET" />
Using this version of Picasso:
implementation 'com.squareup.picasso:picasso:2.71828'
Also it may be worth declaring the Target as a member variable due to issues Picaso has arising from 'weak references'. You will have to research this, but I believe it may be unsafe to declare the Target as an anonymous inner class.
Also it may be necessary to call resize(x, y) to prevent an out of memory situation depending on the image sizes and whether you control their source.
UPDATE:
The project won't work as written because it is using an synchronous solution, but you're making an asynchronous call:
holder.moviePosterIV.setImageBitmap(movie.getPosterBitmap());
The code:
public Bitmap getPosterBitmap() {
target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
posterBitmap = bitmap;
}
#Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
return posterBitmap;
}
It cannot be done like this. In a nut shell, the Target is going to be called in the future when the image has been downloaded. But the code is being written as if the image is ready immediately.
It is a synchronous solution to an asynchronous problem. In synchronous programming we write 1 line after the other then return the result when the data is ready.
If we wrote it synchronously:
f() {
image = getImage(url) // the program could be blocked here for minutes
return image
}
So instead we do it asynchronously:
f() {
getImageInTheFuture(url, imageReadyFunc); // call imageReadyFunc when it is finally downloaded
}
imageReadyFunc(image) {
setTheImage();
}
The asynchronous solution prevents the app from blocking, but it is also a real pain because we can no longer use a 'return' statement. Instead we have to break the code up into 2 sections. Code that we can run before the image is available. Code that we can run after the image is available.
But under the hood Picasso is doing all of this work for you. I'd really advise against trying to manage the bitmaps directly. In the bad old days of Android before Picasso, Glide, etc. apps used to routinely crash and run out of memory trying to manage their own bitmaps. It is technically difficult to do without causing memory leaks and running out of memory.
Hope that makes sense...
The Picasso library allows one to load an image easily like:
Picasso.with(context).load(url).into(imageview);
The API also allows to specify an error image. But what can I do if I want the library to first try three or four different URLs before giving up and displaying the error image? Ideally these images would be tried sequentially, falling back to the next one if the previous wasn't loaded.
Natively there's no API for such functionality. But with some clever coded Picasso.Target you can easily achieve such functionality.
I'll add here a quick hack-untested code that should give you a rought idea on what to look for. You'll have to test and maybe fine tune, but that should be pretty OK.
private static final List<MultiFallBackTarget> TARGETS = new ArrayList<MultiFallBackTarget>();
public static class MultiFallBackTarget implements Picasso.Target {
private WeakReference<ImageView> weakImage;
private List<String> fallbacks;
public MultiFallBackTarget(ImageView image){
weakImage = new WeakReference<>(image);
fallbacks = new ArrayList<String>();
TARGETS.add(this);
}
public void addFallback(String fallbackUrl){
fallbacks.add(fallbackUrl);
}
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from){
removeSelf();
ImageView image = weakImage.get();
if(image == null) return;
image.setImageBitmap(bitmap);
}
public void onBitmapFailed(Drawable errorDrawable){
ImageView image = weakImage.get();
if(image == null) {
removeSelf();
return;
}
if(fallbacks.size() > 0){
String nextUrl = fallbacks.remove(0);
// here you call picasso again
Picasso.with(image.getContext()).load(nextUrl).into(this);
} else {
removeSelf();
}
}
public void onPrepareLoad(Drawable placeHolderDrawable){}
private void removeSelf(){
TARGETS.remove(this);
}
}
Remember that Picasso does not hold strong references to the Target you put inside into(object). That means, internally Picasso uses WeakReference to that.
So that means that you need that self reference in TARGETS to keep reference of all MultiFallBackTarget you create and allow them to self-remove when their job is done.
I'm trying to integrate Processing 3 into a swing application, but because PApplet doesn't extend Applet anymore I can't just add it as a component right away.
Is there anyway of embeding a Processing 3 sketch into Swing, it would be enough if I could just open the sketch in a seperate window without the PDE.
You can run a sketch from Java by extending PApplet and then using the runSketch() function to run that PApplet. It'll look something like this:
String[] args = {"MyPapplet "};
MyPapplet mp = new MyPapplet ();
PApplet.runSketch(args, mp);
public class MyPapplet extends PApplet {
public void settings() {
size(200, 100);
}
public void draw() {
background(255);
fill(0);
ellipse(100, 50, 10, 10);
}
}
Then if you want to get at the underlying component, you have to write code that depends on which renderer you're using. Here's how you'd do it with the standard renderer:
PSurfaceAWT awtSurface = (PSurfaceAWT)mp.surface;
PSurfaceAWT.SmoothCanvas smoothCanvas = (PSurfaceAWT.SmoothCanvas)awtSurface.getNative();
Once you have the SmoothCanvas, you can remove it from its frame and add it to yours.
I try to use the ClientBundle implementation to manage my Images to a large File and minimize the HTTP-Requests.
I put in my gwt.xml
Generate the ClientBundle
public interface ResourceBundle extends ClientBundle {
public static final ResourceBundle INSTANCE = GWT.create(ResourceBundle.class);
#Source("tiles/smiley.png")
ImageResource smiley();
}
The Image would be found, no errors.
Here is the code
#Override
public void onModuleLoad() {
CLogger.log("Start Engine");
int width = 800;
int height = 600;
Canvas canvas = Canvas.createIfSupported();
canvas.setWidth(width + "px");
canvas.setHeight(height + "px");
canvas.setCoordinateSpaceWidth(width);
canvas.setCoordinateSpaceHeight(height);
Context2d c = canvas.getContext2d();
Image img = new Image(ResourceBundle.INSTANCE.smiley());
img.addLoadHandler(new LoadHandler() {
#Override
public void onLoad(LoadEvent event) {
CLogger.log(event.getSource() + " loaded");
}
});
CLogger.log("Visible: " + img.isVisible());
c.drawImage((ImageElement) new Image(ResourceBundle.INSTANCE.smiley()).getElement().cast(), 0, 0);
RootPanel.get().add(canvas);
}
I create a simple Canvas and set the size to 800x600. I create a new Context2D Object to draw the Image at the Context and add the Canvas to the RootPanel.
The logs shows:
[20:10:21.676] - Start Engine
[20:10:21.851] - Visible: true
[20:10:21.853] - http://127.0.0.1:8888/engine/7D39451825E9952050F44A6B8E2E15F3.cache.png
The Image exists under the logged URL so everything looks fine. But the Image would not be draw or it would draw but not display.
Anybody an idea?
I thought the ClientBundle loads the Images as the start in the backend. So if I get an Instance every Image/Css and others fill be loaded?
Regars Markus
Image contents aren't guaranteed to be loaded synchronously. Depending on the capabilities of the browser and the size of the image data, the resource may be stored as a standalone file, which appears to be the case that you're describing (i.e. cache.png). Does a formulation like this make any difference?
final Image img = new Image(ResourceBundle.INSTANCE.smiley());
img.addLoadHandler(new LoadHandler() {
public void onLoad(LoadEvent e) {
c.drawImage((ImageElement) img.getElement());
}
}
RootPanel.get().add(img);
If you use CSSResources and ImageBundles outside of UiBinder you have to make sure that the stlyesheet and images are injected properly.
See here for more information.
i m doing a project on "remote screen capturing and controlling"....in java
it is desktop application...
thre is client-server architecture....here server can capture the clients and make wath on client but, that is not known to client that someone is watching him/her....
and after captureing the client the server can also controlling the client from captured data.....and it it done at client side...automatically...as controlled by server.....
so i want your help ...please give me the suggestion....
Check out the "java.awt.Robot" class:
http://java.sun.com/javase/6/docs/api/java/awt/Robot.html
These methods should help you:
BufferedImage createScreenCapture(Rectangle screenRect);
void keyPress(int keycode)
void keyRelease(int keycode)
void mouseMove(int x, int y)
void mousePress(int buttons)
void mouseRelease(int buttons)
You have in this article the basics of screen capture using Robot (as suggested by brd6644's answer)
We can capture whole desktop, and save it to a PNG file, as follows.
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(screenSize));
ImageIO.write(image, "png", new File(fileName));
}
Alternatively, we might capture our JFrame, including its window decoration, as follows.
public void captureFrame(JFrame frame, String fileName) throws Exception {
BufferedImage image = new Robot().createScreenCapture(frame.getBounds());
ImageIO.write(image, "png", new File(fileName));
}
The old (2003) jxta-remote-desktop project can also give you some pointers