I am not able to write an imageicon in the outputstream .Here is my code.Please anyone help me.
public ScreenSpyer(Socket socket, Robot robot, Rectangle rect) {
this.socket = socket;
this.robot = robot;
this.rectangle = rect;
start();
}
public void run(){
oos = null;
try{
oos = new ObjectOutputStream(socket.getOutputStream());
oos.writeObject(rectangle);
// oos.flush();
// oos.reset();
}catch(IOException ex){
ex.printStackTrace();
}
while(continueLoop){
//Capture screen
image = robot.createScreenCapture(rectangle);
imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
System.out.println("intermidiate");
// oos.reset();
oos.writeObject(imageIcon);
System.out.println("New screenshot sent");
//oos.reset();
//oos.flush();
oos.reset();
} catch (IOException ex) {
ex.printStackTrace();
}
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
You say "it gets stuck"; how do you know? This is obviously a thread, terminated by other code. I assume the trace output "New screenshot sent" does not get executed; that could be because it is stuck, or writeObject() could be throwing an exception you are not catching.
Catch throwable after IOException to see if there's another exception.
Right after your image is generated, replace it with a known image and see if it gets written; that will help figure out if there's a problem with this particular writeObject() call or something wrong elsewhere in the program.
Try using a small rectangle from the screen, instead of all of it. Perhaps getScreenSize() returns something unusable, like something one pixel size larger than the screen. If a small rectangle works, try reducing the rectangle by a few pixels in both dimensions.
It looks like you are actually trying to save the screenshot images to an OutputStream or maybe to disk.
In this case, you don't have to use an ImageIcon. You can save the image that is returned from the createScreenCapture call. You can use ImageIO for saving images:
ImageIO.write(BufferedImage image, String formatName, File output);
or
ImageIO.write(BufferedImage image, String formatName, ImageOutputStream output);
or
ImageIO.write(BufferedImage image, String formatName, OutputStream output);
The formatName can be either jpg, png or gif.
Related
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 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);
}
}
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;
}
Currently there is a method that loads an image from an URL and then processes it like follows:
BufferedImage tempImage;
try {
tempImage = ImageIO.read(url);
}
catch (IOException e) {
return;
}
final BufferedImage image = tempImage;
To clean the code up a bit, I would like to extract the try/catch block while preserving the functionality of exiting the method if the loading of the image fails. I have tried extracting the block as follows:
final BufferedImage image = loadImageFromURL();
if (image == null)
return;
//--------------------------------------
private BufferedImage loadImageFromURL() {
BufferedImage tempImage = null;
try {
tempImage = ImageIO.read(url);
}
catch (IOException e) {
LogUtils.severe(e);
}
return tempImage;
}
The problem is that if the loading of the image fails, the method returns null, which does not make the code any cleaner, as I have to perform an additional null check. I have read through Clean Code: A Handbook of Agile Software Craftsmanship but all the examples seem to rethrow the exception in some form: is that the only/correct way to go?
We don't know the complete situation you are in, but I would just add a throw clause to the method signature and do not use any try catch.
private BufferedImage loadImageFromURL() throws IOException
{
return ImageIO.read(url);
}
Which basically resolves back to the original method, without extracting the try-catch. If the loading fails, the method will stop, but not really return. The exception will go all the way back to some piece of code that handles it properly.
I have data coming from a bluetooth device, the data is being stored in an inputstream. I am taking the data from the inputstream and generating a graphic with Jfreechart. Whenever I turn off the bluetooth device the data keeps coming from the inputstream and the graphic continues being generated.
I need the data and the graphic to stop when I turn off the bluetooth device.
I am using Java.
Every InputStream has a close() method that should do exactly what you need ... if you can detect that the device is turned off, that is.
The docs on this.
Reference link : Closing java InputStream
which resolved my problem too.
Properties props = new Properties();
InputStream fis = new FileInputStream("message.properties");
try {
props.load(fis);
//omitted.
} catch (Exception ex) {
//omitted.
} finally {
try {
fis.close();
fis=null;
} catch (IOException ioex) {
//omitted.
}
}