I need to convert an image file(large size to small size). So, I am decoding image file to bitmap image and then I compressed bitmap image. I need to save this bitmap into File object again. Can anyone help me?
first covert your bitmap to byte[] object by
Bitmap bmp; //your bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Then create a FileOutputStream Object and write the byte[] to the fileoutputstream
File yourOutputFile = new File("/the/path/to/your/file")
FileOutputStream fos = new FileOutputStream(yourOutputFile);
fos.write(byteArray);
fos.close();
return yourOutputFile;//this will be your output
Related
I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();
i want get image From server and put to this image view i want get image from this imageview and compress with bitmap and send this picture to other fragment i dont want add 2 request for my server
my code is :
Bitmap bmp = BitmapFactory.decodeResource(getResources(),"my problem");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
mBundle.putByteArray("ResID", byteArray);
i know if i put R.darwable.ic_luncher it will work
but i want take this image from image view and put to " my probem "
can u help me ?
You can use mImageView.getDrawable() to get Drawable object from ImageView:
Drawable drawable = mImageView.getDrawable();
After that you can obtain a Bitmap object from it:
BitmapDrawable bitmapDrawable = ((BitmapDrawable) drawable);
Bitmap bitmap = bitmapDrawable.getBitmap();
and further, if you want, even InputStream for byte array to send it:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Here, the CompressFormat and quality can be adjusted for output image size
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageBytes = stream.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
As a result, imageBytes will contain all the bytes of the image, which you can send over elsewhere (in your scenario: to your other Fragment)
I need a dynamic solution to convert a unknown image format to .png in java.
Will .getType() help me out here, it seems only to return numbers.
The converted image should later be stored in a folder, but I guess that is easly done in the
ImageIO.write().
It's just that with converting an unknown image format that I have no idea how to approach.
This peace of code should do the magic:
File file = new File("unknown.type.pic");
ByteArrayInputStream bais = new ByteArrayInputStream(FileUtils.readFileToByteArray(file);
BufferedImage image = ImageIO.read(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
OutputStream outputStream = new FileOutputStream ("output.jpg");
baos.writeTo(outputStream);
Add missing try/catch/finally blocks.
I am trying to convert Image to Byte Array and save it in database. Image to Byte Conversion and bytes to Image Conversion using ImageIO Method works completely fine before saving it into database. But when i retrieve bytes from Database ImageIO returns null.
FileInputStream fis = new FileInputStream(picturePath);
BufferedImage image = ImageIO.read(new File(picturePath));
BufferedImage img = Scalr.resize(image, Scalr.Mode.FIT_EXACT, 124, 133, Scalr.OP_ANTIALIAS);
ByteArrayOutputStream ByteStream = new ByteArrayOutputStream();
ImageIO.write(img, "jpg", ByteStream);
ByteStream.flush();
byte[] imageBytes = ByteStream.toByteArray();
ByteStream.close();
PersonImage = imageBytes;
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(PersonImage);
BufferedImage ImageForBox = ImageIO.read((InputStream)byteArrayInputStream);
PersonImageBox.setIcon(new ImageIcon((Image)ImageForBox));
Above code is what I do before saving picture bytes in DB. I resize the Image then convert it into bytes and then convert other way around and show it in JLabel, it works fine. But when I retrieve bytes from database and use the same code i.e.
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(PersonImage); // PersonImage are bytes from dB.
BufferedImage ImageForBox = ImageIO.read((InputStream)byteArrayInputStream);
PersonImageBox.setIcon(new ImageIcon((Image)ImageForBox));
In this case ImageIO returns null. Please help.
I have array[Byte] of file, saved on DB.
Is there a way to convert it to JPG if its for example PNG ?
And no saving file on disc, just operations on those array[byte]
Thanks!
Read it into a BufferedImage and write it again to a byte array, using JPG encoding.
InputStream is = ...;
BufferedImage img = ImageIO.read(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(img, "JPG", baos);
byte[] array = baos.toByteArray();
For the inputstream (is), use either a inputstream that comes straight from the BLOB in your DB, or use a ByteArrayInputStream.