I have been trying to set the album art of a mp3 file using the mp3agic library in java. The following code snippet is what i used to do so. Unfortunately this isnt working. The saved image file does not contain the album art nor the set title. Also, the program compiles with no error.
File img = new File("img.jpg");
imgLink[1]="http://"+imgLink[1];
URL url = new URL(imgLink[1]);
FileUtils.copyURLToFile(url, img);
byte[] bytes = new byte[(int) img.length()];
id3v2Tag.setTitle("Something");
id3v2Tag.setAlbumImage(bytes, "image/jpg");
mp3file.save("something.mp3");
Any help regarding this issue would be much appreciated.
The above code works. It wasn't showing properly on the folder I was working with. When I opened the file with iTunes it showed up. It's probably some fault with windows.
Related
I am attempting to create a program in which the user selects an image from a different folder on their computer and JavaFX copies that image into the project directory for future use. A new folder is created that will store the newly created image file. This is essentially the code for selecting and copying the image into the project directory:
Stage window = (Stage) ap.getScene().getWindow();
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Image File");
File selectedFile = fileChooser.showOpenDialog(window);
//Creates a new directory for the new calendar that the user wants to create
File file = new File("src/DefaultUser/" + nameField.getText() + "/");
file.mkdir();
//Creates a new file name with logo as the name, but keeping the extension the same
int index = selectedFile.getName().lastIndexOf(".");
String ext = selectedFile.getName().substring(index);
//Stored in newFileName
String newFileName = "logo" + ext;
File newFile = new File(file.getPath() + "/" + newFileName);
//Copies the selected file into the project src folder, with newFileName as the new file name
Files.copy(selectedFile.toPath(), newFile.toPath());
Then the program moves onto a different scene and thus a different controller actually loads the image into an ImageView. I know that the path for the Image works properly but for whatever reason the program cannot find the image file to load it into the ImageView.
Here is essentially the code used for that:
image.setImage(new Image("DefaultUser/" + imagePath));
Don't worry about what imagePath is in this case because I am absolutely positive it paths to the correct location for the newly created image file. This is because if I close the JavaFX program and rerun it, the image loads properly.
At first, I thought it was because it took time for the image to be copied into the project directory but I checked that the file actually existed within the code and it did so this is apparently not the case. I tried using Thread.sleep() to delay the program a bit so that the code would potentially have more time to copy the file but it still threw the same error: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found
The strangest part about this is that the program works perfectly fine, it's just that I have to restart the JavaFX program for it to be able to detect the image file, even though I know it exists. Is there something weird about JavaFX and creating new files and accessing them within the same program? I am truly lost. Thank you so much in advance for any help and I'm sorry if this doesn't give enough information because I don't want to have to explain the whole project.
Just like haraldK and James_D said in the comments putting stuff in the src folder is generally a bad idea. I solved the issue by moving the folder out into the project directory instead.
I am trying to make a simple android app that uses OpenAPLR
http://doc.openalpr.com/cloud_api.html
So I copied the code under the Java section on how to make a REST api request and put it all into onClick method of a button and took a picture of a license plate and saved it as license_plate.jpg in the location
app/res/drawable/license_plate.jpg
But whenever I run the application I always get an error pointing to these lines
Path path = Paths.get("drawable/license_plate.jpg");
byte[] data = Files.readAllBytes(path);
java.nio.file.NoSuchFileException: drawable/license_plate.jpg
Where should I be saving this image so I can use it during the application?
And where should I be saving images used for future applications when I am not just using a single picture I have already preloaded?
You give wrong path. You can only save a picture in your device and
then give image folder path for example :
Path path = Paths.get("/storage/projects/alpr/samples/testing/car1.jpg");
byte[] data = Files.readAllBytes(path);
In my java package, I have a file called 'prog.ico'. I'm trying to load this file, via the following code:
java.net.URL url = this.getClass().getResource("prog.ico");
java.awt.Image image = ImageIO.read( url );
System.out.println("image: " + image);
This gives the output:
image: null
What am I doing wrong? The .ico file exists in the same package as the class from which I'm running this code.
It seems that the .ico image format is not supported. See this question and it's answer to get around this.
To prevent link rot: This solution recommends using Image4J to process .ico files.
I've written a plugin for ImageIO that adds support for .ICO (MS Windows Icon) and .CUR (MS Windows Cursor) formats.
You can get it from GitHub here: https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-ico
After you have it installed the plugin, you should be able to read your icon using the code in your original post.
I thing you must go over FileInputStream to wrap the file
File file = new File("prog.ico");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file
Im about to start working with tesseract, tess4j to be exact, and im going through the api docs. I havent come across any way to read from a webpage.
Basically the program opens a webpage that is just an image. I want tess4j to read the image from the page and convert it to words. If tess4j cant do this is there any other java ocrs that would be helpful, preferably without downloading the image?
Appreciate the help.
Try this:
String imageURL = "<Remote URL of image>";
String result = "";
URL url = new URL(imageURL);
BufferedImage img = ImageIO.read(url);
Tesseract instance = new Tesseract();
instance.setDatapath("<your tessdata path>");
result = instance.doOCR(img);
Reading and downloading are synonymous. If you are looking into reading an image from a web-page without opening it in a web-page, I would suggest looking into the "curl" command and it's equivalent in Java. After getting the image with the aforementioned command, it can then be parsed with Tesseract.
I have an mp3 file that i am trying to play from a source package in eclipse (java) for some reason it plays fine when i run it in eclipse but when i try to play it from a .jar it doesn't play. The images show up fine but the sound doesn't, i think i may have to recode the sound file like an image file but i'm not sure how here's the code for the Image:
JLabel TextBox = new JLabel(new ImageIcon(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/Images/TextBox.png"))));
and the Sound
Player player = Manager.createPlayer(new MediaLocator(getClass().getClassLoader().getResource("Sounds/Bonsai.mp3")));
thanks in advance!
Try getting the resource as a Stream. You can retrieve a file this way.
InputStream fis =
ClassLoader.getSystemClassLoader().getResourceAsStream("/Sounds/bonsai.mp3");
p = new Player(fis);
p.play();
The:
getClass().getClassLoader().getResource(....)
does indeed return a URL. But MediaLocator can not use URL's from the form jar:file:/
I recommend to copy the file from the jar to a "Temp" filesystem and the stream it from there.
You can always delete it when you done, if that is needed.