Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I read an image from URL?  RSS feed

 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-edit&clip_id=3.2017-08-02.72&target=wb2¬b=1&img=//192.168.2.18/pm8/data/pmin/2017/201708/20170802/cuts/141.jpg  ; this is the url in my application i want to read an image from this and display it.
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like javax.imageio.ImageIO.read(new URL(...).openStream()) should do the trick by handing you a BufferedImage object.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it will work if i had to give the complete url with image but first i have to find image then display it
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you do have the URL - at least that's what you said in your post.

If you're now asking about having to "find" the image first, you need to elaborate what that means.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://192.168.2.18/a3/host/pmms.ankiti.com/ this is the URL of my application in this when I log in after that I click on headline then in one panel it will open the image and one panel with the browser but the problem is it's not displaying image by clicking the headline.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
\\192.168.2.18\pm8\data\pmin\2017\201708\20170802\cuts this is the path where it fetch from and display the image
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the problem is it's not displaying image by clicking the headline.

You didn't say anything about how the click is handled, or how the image is supposed to be displayed, so we don't have a basis even for guessing what might be going wrong. If you have code to do this that somehow doesn't work, post relevant excerpts. If there is an error message, post it. TellTheDetails
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there is any way to display image on one panel and text on other in single frame with webview in java
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean the Webview that's part of JavaFX? If so, it depends on what you mean by "panel". That's not a term that has a fixed meaning in web development.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically my ques was
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-edit&clip_id=73.2017-08-04.8&target=wb2¬b=1&img=//192.168.2.18/pm8/data/pmin/2017/201708/20170804/cuts/4534.jpg i want to parse url to get image with file name so im using condition if url =("&img") {
url.split("=");
system.out.println(url);
} but with this condition neither im getting output nor a error
 
Dave Tolls
Rancher
Posts: 2914
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show the actual code doing that check as this:

is incorrect in a number of ways.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sry I wrote wrong by mistake if url("&img=")
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's still not useful code.
Can you please just post some of the code that does this, and wrap it in code tags.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from this im getting path of the image with image name but it's not loading in JavaFX and if I'm doing as hardcode with the different program with just passing image path and name im getting the image so please tell whats the problem
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the output
run:
http://192.168.2.18/a3/host/pmms.ankiti.com/?
http://192.168.2.18/a3/host/pmms.ankiti.com/?
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-workdate
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-workdate
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-ini&noob=1&noshell=1&ini=rajveersingh&output_format=html

...

http://192.168.2.18/a3/host/pmms.ankiti.com/?win=IBB&module=mm-pm-headlines
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-headlines&do=subject&subject_uid=Z10
http://192.168.2.18/a3/host/pmms.ankiti.com/?module=mm-pm-edit&clip_id=67.2017-08-09.70&target=wb2¬b=1&img=//192.168.2.18/pm8/data/pmin/2017/201708/20170809/cuts/2895.jpg
110
string length is: 173
//192.168.2.18/pm8/data/pmin/2017/201708/20170809/cuts/2895.jpg
file:////192.168.2.18/pm8/data/pmin/2017/201708/20170809/cuts/2895.jpg
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
file:////192.168.2.18/pm8/data/pmin/2017/201708/20170809/cuts/2895.jpg

Is that a valid URL? Meaning, is there an image at that location? It would be extremely unusual to have a top-level directory called "192.168.2.18" on the disk, but I suppose it's just about possible. You should check whether that file exists before using it.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should the protocol be http vs file on line 71?
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had changed and try with http also
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How have you ascertained that the ImageIcon class can handle either the "file" or the "http" protocol?
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i had check by  passing the url with file by making adiiferent program in that program i had just added frame and in that it is loaded with file:
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
either JavaFX have the concurrency issue because everything is working in the same thread
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Niti Kapoor wrote:i had check by  passing the url with file by making adiiferent program in that program i had just added frame and in that it is loaded with file:
  

OK. And "/192.168.2.18/pm8/data/pmin/2017/201708/20170809/cuts/2895.jpg" is the correct path to a file on your file system? It will only work on a Unix-based OS (such as Linux and OS X), and on those it would be extremely unlikely to have such a path.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i would not work on windows hows that possible
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package imagejafx;

/**
*
* @author biznis
*/




import java.util.ArrayList;
import javafx.application.Application;
import javafx.geometry.HPos;
import javafx.geometry.VPos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;


public class WebViewSample extends Application {
    private Scene scene;
    @Override public void start(Stage stage) {
        // create the scene
        stage.setTitle("Web View");
        scene = new Scene(new Browser(),750,500, Color.web("#666970"));
        stage.setScene(scene);
        //scene.getStylesheets().add("webviewsample/BrowserToolbar.css");

        stage.show();

//        scene.addEventFilter(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
//            @Override
//            public void handle(MouseEvent mouseEvent) {
//                System.out.println("mouse click detected! " + mouseEvent.getSource());
//            }
//        });
    }

    public static void main(String[] args){
        launch(args);
    }
}
class Browser extends Region {

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    public Browser() {
        //apply the styles
        getStyleClass().add("browser");
        // load the web page
        int i=0;
        //webEngine.load("https://www.google.it/earth/images/products_mobile.png");

        String first ="<html> <body>";
        String fin ="</body>  </html>";
        String complete = "";


        ArrayList<String> test = new ArrayList<>();
        test.add("file://192.168.2.18/pm8/data/pmin/2017/201708/20170812/cuts/2590.jpg");
     



        complete = complete.concat(first);
        for(int j = 0; j< test.size(); j++)
        {

            complete = complete.concat("<img src=");
            complete = complete.concat(test.get(j));
            complete = complete.concat(" style= padding:10px; margin:20px; display:inline-block; margin: 0px 12px 12px 0px; overflow: ; position: relative; vertical-align: top; z-index: 5; height:200px width:auto border:6px solid red>");



        }


        complete = complete.concat(fin);

        browser.getEngine().loadContent(complete);


        //add the web view to the scene
        getChildren().add(browser);


    }
    private Node createSpacer() {
        Region spacer = new Region();
        HBox.setHgrow(spacer, Priority.ALWAYS);
        return spacer;
    }

    @Override protected void layoutChildren() {
        double w = getWidth();
        double h = getHeight();
        layoutInArea(browser,0,0,w,h,0, HPos.CENTER, VPos.CENTER);
    }

    @Override protected double computePrefWidth(double height) {
        return 750;
    }

    @Override protected double computePrefHeight(double width) {
        return 500;
    }
see i had added the url in other program to debug its running
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that code supposed to show? And why are you not using code tags to properly display the code?

It would not work on Windows because that is not a Windows file path. I can't tell from your posts whether you understand tat the "file" protocol does not access a remote file over the network, but rather a file on the local file system. I also can't tell whether you have stored that image file on the local file system. Start by providing lots more details (not code) about what you have and have not done, and where you're stuck making progress.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had mentioned above that I had made a web view for just debugging the URL of the image and its running.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no, the file is stored over the network that why the URL is starting with 192.168.2.28
 
Tim Moores
Saloon Keeper
Posts: 3889
91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the file URL pointing to a remote location works, and you can use the ImageIcon class to display it. OK. So what is the actual problem you're facing now? Please post the shortest possible code that demonstrates the actual issue (no comments, no unnecessary imports, no unnecessary GUI stuff etc. - just the very basics). Be sure to use code tags to properly format the code - from now on, I will no longer respond to questions containing unformatted code.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(trgurl.matches("(.*)img=(.*)")){
                      
                      int n = trgurl.indexOf("&img=");
                      System.out.println(n+5);
                     int len = trgurl.length();
                      System.out.println("string length is: "+trgurl.length());
                      System.out.println(trgurl.substring(n+5,len));
                      String find = "file://"+trgurl.substring(n+5,len);
                      System.out.println(find);
                       ImageIcon ii = new ImageIcon(find);
                      StackPane sp = new StackPane();
                      javafx.scene.image.Image img = new javafx.scene.image.Image(find);
                      ImageView imgView = new ImageView(img);
                      sp.getChildren().add(imgView);
                      Scene scene = new Scene(sp);
                      
                     webEngine.load("http://pmms.ankiti.com/");
    webEngine.load("http://192.168.2.18/a3/host/pmms.ankiti.com/?");
                    }        problem is here when im extracting url of image from the application which is running in webview  than in one panel the content should be run and in other image should be displayed but its nt displaying in this and when im passing url in other program without loading the application than the image is read
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use the [Preview] button (to the left of the Submit button) BEFORE hitting the Submit button to make sure the code has been properly wrapped with code tags.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok norm i will remember your point from now and the problem is that im just loading a url ie http://192.168.2.18/a3/host/pmms.ankiti.com/? which shows cntent in one frame and image in one frame but im only getting content not image hope this is clear
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this i a Screenshot what i want.
WebviewWithimage.png
[Thumbnail for WebviewWithimage.png]
this is the screenshot what i want
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post a small, simple, complete and correct example program that compiles and executes and shows the problem for testing?
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is screenshot which im getting after running the program means the image is not loading
WithoutImage.png
[Thumbnail for WithoutImage.png]
this is my output screenshot
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what i posted above the compiles fine and its correct program norm
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post a small, simple, complete and correct example program that compiles and executes and shows the problem for testing?
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It has more logic than is needed for testing the loading of an image.  Get rid of everything not needed to test for the loading of the image.
 
Niti Kapoor
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[code=java][/code]import java.awt.BorderLayout;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.ObservableList;
import javafx.concurrent.Worker;
import javafx.concurrent.Worker.State;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class Main extends Application {
  @Override
  public void start(final Stage stage) {
    stage.setWidth(400);
    stage.setHeight(500);
   Scene scene = new Scene(new Group());
    //Group root = new Group(); 
       // Scene scene = new Scene(root,80,20);

    final WebView browser = new WebView();
    final WebEngine webEngine = browser.getEngine();

    ScrollPane scrollPane = new ScrollPane();
    scrollPane.setContent(browser);

    webEngine.getLoadWorker().stateProperty()
        .addListener(new ChangeListener<State>() {
          @Override
          public void changed(ObservableValue ov, State oldState, State newState) {

            if (newState == Worker.State.SUCCEEDED) {
              stage.setTitle(webEngine.getLocation());
stage.setTitle(webEngine.getLocation());
              String trgurl=webEngine.getLocation();
              System.out.println(trgurl);
                  if(trgurl.matches("(.*)img=(.*)")){
                      
                      int n = trgurl.indexOf("&img=");
                      //String str1 = Integer.toString(n);
                      System.out.println(n+7);
                      int len = trgurl.length();
                      System.out.println("string length is: "+trgurl.length());
                      System.out.println(trgurl.substring(n+7,len));
                      String find = "file://"+trgurl.substring(n+7,len);
                      System.out.println(find);
                  try {
                      new File(find).toURI().toURL();
                  }
                  catch(Exception e){
                     
                  }
                  }
            }
           
            }


        });
    webEngine.load("http://pmms.ankiti.com/");
  //  webEngine.load("http://192.168.2.28/a/apps/pmms/");
    scene.setRoot(scrollPane);

    stage.setScene(scene);
    stage.show();
    stage.setResizable(true); 
   // jfxPanel.setScene(scene);
   
  }

 
private static void extracted(String[] args) {
launch(args);
//JFXPanel jfxPanel  = new JFXPanel();
}
}
This is a small code
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you forgot what I suggested:

 
[Post New]posted 8/12/2017 9:29:18 AM Quote
Please use the [Preview] button (to the left of the Submit button) BEFORE hitting the Submit button to make sure the code has been properly wrapped with code tags.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!