Is it possible to modify a file path so that it incorporates a String variable?
example code
String var = "Picture.jpg";
ImageIcon img = new ImageIcon("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\var");
Use String.format
String var = "Picture.jpg";
ImageIcon img = new ImageIcon(String.format("C:\\Users\\Main\\Documents\\CompSciProjects\\Memory Game\\%s", var));
Related
I have used JNA library and this small API (JnaFileChooser)
https://github.com/steos/jnafilechooser
JnaFileChooser fc = new JnaFileChooser();
fc.addFilter("All Files", "*");
fc.addFilter("Pictures", "jpg", "jpeg", "png", "gif", "bmp");
if (fc.showDialog(parent)) {
File f = fc.getSelectedFile();
// do something with f
}
But how do I use JNA to access this dialg "select folder"
The entire dialogue is controlled on the native side. The package you are are using is already accessing that dialog and that button.
Tracing through the source code of the JnaFileChooser class, this dialogue is part of the WindowsFolderBrowser class. The dialog appears using the SHBrowseForFolder() function combined with SHGetPathFromIDList, and returns the path when the Select Folder button is pressed.
final Pointer pidl = Shell32.SHBrowseForFolder(params);
if (pidl != null)
// MAX_PATH is 260 on Windows XP x32 so 4kB should
// be more than big enough
final Pointer path = new Memory(1024 * 4);
Shell32.SHGetPathFromIDListW(pidl, path);
final String filePath = path.getWideString(0);
final File file = new File(filePath);
Ole32.CoTaskMemFree(pidl);
return file;
}
The params variable passed to this function is of native type BROWSEINFO which controls the dialog box. You can see in the code how a few things have been assigned to it (abbreviated version of code):
final Shell32.BrowseInfo params = new Shell32.BrowseInfo();
params.hwndOwner = Native.getWindowPointer(parent);
params.ulFlags = Shell32.BIF_RETURNONLYFSDIRS | Shell32.BIF_USENEWUI;
params.lpszTitle = title;
If you want to change anything else about the dialog, you need to use a callback. One of the elements in BROWSEINFO is BFFCALLBACK lpfn; where you would define that function, e.g., params.lpfn = the defined callback function.
Documentation for BFFCALLBACK indicates you'll use the option to use SendMessage to change the OK button text with BFFM_SETOKTEXT.
is there anyway to write image in freemarker instead of giving link as
<img src="${pathToPortalImage}
Note : cant we use otputstream or something in freemarker ?
You can embed the image as base64 directly inside the html img tag.
To convert an image to base 64 you can use Apache Commons (codec).
Here is a solution using Apache Commons IO + Codec (but you can do without if you want):
File img = new File("file.png");
byte[] imgBytes = IOUtils.toByteArray(new FileInputStream(img));
byte[] imgBytesAsBase64 = Base64.encodeBase64(imgBytes);
String imgDataAsBase64 = new String(imgBytesAsBase64);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
Then pass the variable imgAsBase64 into the Freemarker context, and use it like this:
<img alt="My image" src="${imgAsBase64}" />
A great example above. But with JAVA 8 we can do something like this:
Path path = Paths.get("image.png");
byte[] data = Files.readAllBytes(path);
byte[] encoded = Base64.getEncoder().encode(data);
String imgDataAsBase64 = new String(encoded);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
private String encodeImage(byte[] imageByteArray, String fileType) {
return "data:" + fileType + ";base64," + Base64.getEncoder().encodeToString(imageByteArray);
}
use output in below tag
<img src="[OUTPUT_OF_ABOVE_METHOD]">
I need to do to a String manipulation. Initially I will be getting an image path as one of the below:
image = images/registration/student.gif
image = images/registration/student_selected.gif
image = images/registration/student_highlighted.gif
and I need to manipulate the string image path to get 2 different image paths.
One is to get the path as:
image1 = images/registration/student.gif
for that I used the function below:
private String getImage1(final String image) {
String image1 = image;
image1 = image.replace("_highlight", "");
image1 = image.replace("_selected", "");
return image1;
}
the second image path I need is to get the path:
image2 = image = images/registration/student_selected.gif
the function I used to get the image2 output was:
private String getImage2(final String image) {
String image2 = image;
boolean hasUndersore = image2.matches("_");
if (hasUndersore) {
image2 = image2.replace("highlight", "selected");
} else {
String[] words = image2.split("\\.");
image2 = words[0].concat("_selected.") + words[1];
}
return image2;
}
But the above methods didn't give me the expected result. Can anyone help me with it?
Thanks a lot!
you could use indexOf(...) instead of match(). match will check the whole string against the regex.
for (final String image : new String[] { "images/registration/student.gif", "images/registration/student_highlight.gif",
"images/registration/student_selected.gif" }) {
String image2 = image;
final boolean hasUndersore = image2.indexOf("_") > 0;
if (hasUndersore) {
image2 = image2.replaceAll("_highlight(\\.[^\\.]+)$", "_selected$1");
} else {
final String[] words = image2.split("\\.");
image2 = words[0].concat("_selected.") + words[1];
}
System.out.println(image2);
}
this will give you expected output.
btw, i changed replaceAll(..) regex, since the image filename could have string "highlight" as well. e.g. stuhighlight_highlight.jpg
If I understood correctly, you need below outputs from respective functions
"images/registration/student.gif"-> getImage1(String)
"images/registration/student_selected.gif" -> getImage2(String)
Assuming above output, there are few mistakes in the both functions
getImage1()->
In the second replace you need to use image1 variable which is output of first replace.
You need to replace "_highlighted" and not "_highlight"
getImage2()->
If you need to search for '_' then use indexOf function.
You need to replace 'highlighted' not 'highlight'
I have modified the functions as below which gives required output
private static String getImage1(final String image) {
return image.replace("_highlighted", "").replace("_selected", "");
}
private static String getImage2(final String image) {
if (image.indexOf("_")!=-1) {
return image.replace("highlighted", "selected");
} else {
return image.replace(".", "_selected.");
}
}
First, getImage1() is probably not doing what you want it to do. You are assigning a value to the variable image1 3 times. Obviously the last assigned value gets returned.
Second, image2.matches("_") will not tell you if image2 contains an underscore (which I think is what you're trying to do here).
I suggest to do some more testing/debugging yourself first.
1.
private String getImage1(final String image) {
return image.replaceAll("_(highlighted|selected)", "");
}
2.
private String getImage2(final String image) {
return image.replaceAll(getImage1(image).replaceAll("(?=.gif)", "_selected");
}
I have a JTextPane and I do it :
HTMLEditorKit kit = new HTMLEditorKit();
HTMLDocument doc = new HTMLDocument();`
this.setEditorKit(kit);
this.setDocument(doc);
Then I do :
profilePictureSrc = "http://ola/profilePicture1.jpg";
chatContent ="<img src=\"" + profilePictureSrc + "\">";
Where profilePictureSrc is a URL Object.
It works but I must use a String instead of the URL (Java Hashtable put method slow down my application)
How Can I do that ? Do I have to put the picture files somewhere and use a relative Path to reach them ? Thank you very much for your ideas
Best Regards
You can convert url objects to strings.
String urlstring = myurl.toString();
Is it possible to get the name of an image in java. The image source is a url.
For eg. "http://172.16.2.42/apache_pb.png"
I need the output "apache_pb.png"
String s = "http://172.16.2.42/apache_pb.png";
int index = s.lastIndexOf('/');
String name = s.substring(index+1);
System.out.println(name);
You can use this helper method from the URL class:
String file = new URL("http://172.16.2.42/apache_pb.png").getPath();
Would URL#getFile() do this for you?