I need to convert a fits file to jpg in java. I've tried with imageJ but I need a simple library without GUI. I'm developing a web page in java and I need to convert the file in background (headlessly).
Alex solved it by doing this:
public void fitsToJpg(String source, String destination, String image){
try {
ImagePlus imageP = openImage(source+image);
final File out = new File(destination+"preview.jpg");
BufferedImage imagen = imageP.getBufferedImage();
ImageIO.write(imagen, "jpg", out);
} catch (IOException ex) {
Logger.getLogger(Fits.class.getName()).log(Level.SEVERE, null, ex);
}
}
Related
Hello and thank you for your help in advance.
I'm working on an rpg character builder. Each character is stored as a JSON file and I would like to include the character image. This was stored as an ImageIcon, but it can't be stored in a JSON.
How can I convert the ImageIcon to a String and back again?
This is what I have so far:
public ImageIcon getImageIcon() {
byte b[];
ByteArrayInputStream bi;
ObjectInputStream si;
ImageIcon image = null;
try {
b = this.imageIcon.getBytes();
bi = new ByteArrayInputStream(b);
si = new ObjectInputStream(bi);
image = (ImageIcon) si.readObject();
} catch (IOException | ClassNotFoundException ex) {
System.out.println(ex);
}
return image;
}
public void setImageIcon(ImageIcon imageIconIn) {
ByteArrayOutputStream bo;
ObjectOutputStream so;
try{
bo = new ByteArrayOutputStream();
so = new ObjectOutputStream(bo);
so.writeObject(imageIconIn);
so.flush();
this.imageIcon = bo.toString();
} catch (IOException ex){
System.out.println(ex);
}
}
The problem is I get the following error when converting the String back to an ImageIcon:
java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
I think the setImageIcon() method is fine, but I'm not sure how to fix the getImageIcon() method.
I'm using Java 1.8 and GSON 2.8.0
Thanks again for your help.
Your icon should be either stored:
inline in your JSON file as a String using Base64 encoding.
see this thread
in a separate file, and the filename stored as a String in the JSON.
{ "iconFile": "./characters/dwarf.png" }
I am trying to write and read a bitmap following the suggestions on other topics about this, the thing is i never get the bitmap when i try to read on the path where i saved the image:
So i have this to write the bitmap:
private String saveToInternalStorage(Bitmap bitmapImage){
ContextWrapper cw = new ContextWrapper(getApplicationContext());
// path to /data/data/yourapp/app_data/imageDir
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
// Create imageDir
File mypath=new File(directory,"captured");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(mypath);
// Use the compress method on the BitMap object to write image to the OutputStream
bitmapImage.compress(Bitmap.CompressFormat.JPEG, 100, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return directory.getAbsolutePath();
}
}
i pass the returned path to another activity and then i pass it as parameter to get the bitmap like this:
private void loadImageFromStorage(String path)
{
try {
File f=new File(path, "captured.jpg");
Log.d("filehe",f.toString());
b = BitmapFactory.decodeStream(new FileInputStream(f));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
i feel i am doing something wrong here, but can't figure out what, the b variavel has no value :/.
Any help?
Thanks
So i have this to write the bitmap:
The file that you save is named captured.
i pass the returned path to another activity and then i pass it as parameter to get the bitmap like this
Here, you are trying to load captured.jpg, which is not captured.
You could avoid this sort of problem by having the first method return the File that the second method then uses.
Also:
Use an image-loading library (e.g., Picasso, Glide) that has an in-memory cache, so you do not waste the user's time re-loading the same bitmap from disk
Get rid of ContextWrapper from the first method, as you do not need it
I am using JPedal free version jar to render PDF in my java swing program. The normal PDF is getting rendered properly but while rendering the PDF image(Image file converted to PDF file) the quality drops considerably(not readable at all).
Example code :
public OpenViewer() {
//create and initialise JPedal viewer component
final Viewer myViewer =new Viewer();
myViewer.setupViewer();
//code to open when required
final File file=null; //example is commented out below
final InputStream stream = null;
//open the stream or File
try {
file = new File("/Users/markee/Desktop/myfile.pdf"); // This PDF is converted from tiff
} catch (Exception e) {
e.printStackTrace();
}
if(file!=null) {
myViewer.executeCommand(Commands.OPENFILE, new Object[]{file});
}
}
I'm currently trying working on an own game and created a Animation class, my problem is that i want the programm to be able to still find all the images when i create a jar out of it so I tried to load an Image via
BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream("player.png"));
but when I start the code I get a NullPointerException, i checked the location twice but the image exists and there should be no problems, can anyone help me out a bit?
try this
public BufferedImage loadImage(String fileName){
BufferedImage buff = null;
try {
buff = ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buff;
}
I have created an applet jar. That jar contains an images in the following folder
com\common\images\red.bmp
Now, I want to display this image on the Swing Applet.
private static final ImageIcon redIndicator = new ImageIcon("com\\common\\images\\red.bmp");
After that, I have attached the redIndicator to a JPanel but I am not able to see this image.
Any suggestions?
==================================EDITED=========================================
private static final ImageIcon marker = loadImage("com/common/images/scale.jpg");
#SuppressWarnings("unused")
private static ImageIcon loadImage(String imagePath) {
BufferedInputStream imgStream = new BufferedInputStream(TpcHandler.class.getResourceAsStream(imagePath));
int count = 0;
if (imgStream != null) {
byte buf[] = new byte[2400];
try {
count = imgStream.read(buf);
} catch (java.io.IOException ioe) {
return null;
} finally {
if (imgStream != null)
try {
imgStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (count <= 0) {
LOGGER.warning("Empty image file: " + imagePath);
return null;
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
} else {
LOGGER.warning("Couldn't find image file: " + imagePath);
return null;
}
}
I am getting the following exception
java.io.IOException: Stream closed
at line count = imgStream.read(buf);
This should do the trick (if called from a class loaded from that same jar):
new ImageIcon(getClass().getResource("/com/common/images/red.bmp"))
Use YourPanel.class.getResourceAsStream("/com/common/images/red.bmp"), read the stream to a byte[] and construct the ImageIcon based on that. (and don't use bmps - prefer png or jpeg)
Applets and Images that is a frequently asked questions so, as for Java applets and images, I recommend you read one of my previous answers hope it helps a bit :)
Good luck