Get bitmap image from file - java

I need to save a captured image from camera on a tmp file and retrieve it on the next activity, so i can handle better the OOM error.
So i did something like this to write the file:
public void savePicAtFile(Bitmap pic){
FileOutputStream out = null;
try {
out = new FileOutputStream("tmpCameraPic");
pic.compress(Bitmap.CompressFormat.JPEG, 100, out); // bmp is your Bitmap instance
// PNG is a lossless format, the compression factor (100) is ignored
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
now i don't know how to retrieve the compressed image, should i just create a bitmap and getall the stream?
Any help?

Related

Download image from URL keeping exif data

I'm trying to download an image with exif data from URL to my pc, the problem is that exif data is gone after the image has been downloaded.
here's the code i use
public static void downloadImage(String str) {
try {
URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/6/68/Goldmantelziesel.jpg");
BufferedImage img = ImageIO.read(url);
File file = new File("F:\\Photos\\ww123\\"+str+".jpg");
ImageIO.write(img, "jpg", file);
} catch (MalformedURLException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}

Android pdf maker code error

I'm trying to take a screenshot of my Android activity (bitmap) and then making a PDF file out of it. I used the itextpdf library. Here's what I have:
public void onSaveDataClicked(View reportsLayout){
//take screen shot
Bitmap screen; View v1 = reportsLayout.getRootView();
v1.setDrawingCacheEnabled(true);
screen = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(file));
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
screen.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
addImage(document,byteArray);
document.close();
}
catch (Exception e){
e.printStackTrace();
}
}
private static void addImage(Document document,byte[] byteArray) {
Image image = null;
try {
image = Image.getInstance(byteArray);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// image.scaleAbsolute(150f, 150f);
try {
document.add(image);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
When I try to run I get the errors: "Image cannot be converted to Element" and "cannot find symbol method getInstance(byte[])"
A lot of the code I found online through a tutorial on how to achieve this. I'm kind of unfamiliar with this sort of thing. Any help and ideas would be appreciated.
Use import com.itextpdf.text.Image;and remove the default Android one.

Capture a specified area of screen using java

I am implementing a screenshot capture in my Java web Application Which Is Already Working. I however have an issue with the screenshot because the Image is Taken for the entire Screen:
As you can see this is the entire screen, I just want to capture the area with the graphs(the White area). The Code I am using to capture this screen is:
public void captureGraphs() {
try {
Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
Robot robot = new Robot();
BufferedImage img = robot.createScreenCapture(new Rectangle(size));
File f = new File("C:/capture");
if (!f.exists()) {
f.mkdir();
}
File[] flist = f.listFiles();
for (File flist1 : flist) {
flist1.delete();
}
ImageIO.write(img, "JPG", new File("C:/capture/screenShot.jpg"));
System.out.println("Capture Successfull");
FacesContext.getCurrentInstance().addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Successful!",
"You have successfully taken a snapshot. Thank You"));
} catch (HeadlessException e) {
System.out.println(e.getMessage());
} catch (AWTException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
Is there any way I can change this code to capture the Mid Part only holding the graphs??

BufferedImage goes black when used as method parmeter

I've got what seems like a very simple section of code, and I can't work out for the life of me why it's not working.
I have a method that listens for image updates from a camera and when it recieves them it calls another segment of code.
My listener is:
public void imageUpdated(BufferedImage image) {
if (null != video) {
video.setImage(image);
}
File outputfile = new File("savedingui.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
Which happily saves the correct image to disc. However when I save the image again from the setImage method (called on line 3 of the listener code)
public void setImage(BufferedImage image) {
File outputfile = new File("savedorig.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
It now just saves a jpeg of black. But the right sized square of black.
Any clues as to whats going on?
I can not reproduce your issue with the following source (which is basically copied from your question):
public static void imageUpdated(BufferedImage image) {
setImage(image);
File outputfile = new File("savedingui.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setImage(BufferedImage image) {
File outputfile = new File("savedorig.jpg");
try {
ImageIO.write(image, "jpg", outputfile);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException {
BufferedImage image = ImageIO.read(new File("test.jpg"));
imageUpdated(image);
}
Is the same instance used somewhere else, e.g. camera writing updated data in it?

Error in Sending captured image from Java to Android

I have the code to capture image from screen in java, I have the final captured image as BufferedImage object and Can cast it to ImageIcon
The problem is when sending that file to android can't read it as bitmap drawable. Any one have answer to this ?
Code to send (Java)
BufferedImage image = robot.createScreenCapture(rectangle);
ImageIcon imageIcon = new ImageIcon(image);
//Send captured screen to the server
try {
System.out.println("before sending image");
oos.writeObject(imageIcon);
oos.reset(); //Clear ObjectOutputStream cache
System.out.println("New screenshot sent");
} catch (IOException ex) {
ex.printStackTrace();
}
Android Receiver Part
Thread t= new Thread(new Runnable() {
#Override
public void run() {
while (true) {
try {
client= sc.accept();
is = client.getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapDrawable imageIcon = null;
try {
ois = new ObjectInputStream(is);
imageIcon = (BitmapDrawable) ois.readObject();
//Drawable d = Drawable.createFromStream(is, null);
IV.setImageDrawable(imageIcon);
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("New image recieved");
}
}
I get the exception of it can't cast the imageIcon or the BufferedImage to Bitmap drawable.
You should not use objects, because oracle jvm is not the same as android's dalvik.
Try to convert captured image to raw bytes and send those bytes to android. Then in android covert this raw data to drawable.

Categories