I am using the following code for encoding my bitmap:
public static String convertBitmapToBase64String(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
String encodedImage = null;
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
encodedImage = Base64
.encodeToString(byteArrayImage, Base64.DEFAULT);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return encodedImage;
}
But this method returns a very long string and therefore I can't upload my image to the server.
Please help.
Thanks in advance.
There is nothing base64 can do for the actual size. The encoding will inevitably increase the size by a fixed percentage (about 37% IIRC).
The only thing you can do is make the image data smaller. You are using an unreasonable quality setting here:
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
100% quality JPEG is overkill, 83-90% is more reasonable for most images. If you need lossless image quality, try PNG. JPEG isn't designed for 100% quality, the resulting file will very likely be larger than if you used a lossless image format in the first place.
if your image is really too big you can always use Bitmap.createScaledBitmap() to resize it smaller
Related
I want to convert my app images to base64 so it won't show on the gallery. I have tried various techniques. The image is from a zip file so it is a byte array at that point. The length of the base64 change when I change the byte array size. What is the proper byte array size? And the base64 encoded image doesn't work too.
The primary code is
String encodedImage = Base64.encodeToString(buffer, Base64.DEFAULT);
buffer is the byte array(102400)
and it contains the image too.
The image is a 7KB file and the output is 400KB
You may try following function to convert image into Base64:
public void toStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
}
Above function takes Bitmap image and converts it into Base64 encoded string. This is working in my project and I hope this will help you too.
I want to use Gravatar but I don't want to publish users MD5 hashes of their e-mail addresses. And there is more potential problems. So I decided to download them and store them in my database.
But I have a problem with such a simple task as my profile picture (Earlybird) looks bad after downloading:
This is the code I used.
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
final URL url = new URL("http://www.gravatar.com/avatar/" + account.getGravatarHash() + "?d=identicon");
final BufferedImage image = ImageIO.read(url);
ImageIO.write(image, "jpg", baos);
pic = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
Value in pic is then directly stored to the database. Other pictures in my database are stored fine so problem must be in these lines.
EDIT:
I just partially fix the problem by changing "jpg" for a "png" even thought Gravatar tutorial is mentioning "jpg". Also I don't want to specify image format (unless all Gravatars are png). Can I avoid that? I want just save the bytes I get.
Browsers in most cases work with raw bytes. However it is highly appreciated to send "Content-Type: image/..." header for each image.
When you save bytes in DB you also have to
either save image content type, provided by Gravatar for this image or
convert image into your default format, so you can to hardcode content type for all images from your DB
To get headers, provided by Gravatar, you can use Apache HTTP Client.
To convert image into your preferred format, you can use ImageIO.
I found one similar problem with working solution:
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()){
final URL url = new URL("http://www.gravatar.com/avatar/" + account.getGravatarHash() + "?d=identicon");
InputStream inputStream = url.openStream();
byte[] buffer = new byte[1024];
int n;
while (-1 != (n = inputStream.read(buffer))) {
baos.write(buffer, 0, n);
}
inputStream.close();
pic = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
}
Looks like this works with png and jpg Gravatars.
I am trying to find a solution to be able to download an image from Url into the Filesystem and resize it without the need to decode it to a bitmap first.
The only solution I could find yet was the approach to decode it to a bitmap with specific SampleSize. However, the problem here is that a lot of memory is consumed when creating the bitmap which would limit the maximum size of the image depending on the free memory available on the device. Another Problem is that it takes a lot of time to decode it to a Bitmap and also to compress it back to an output stream.
My current code looks like this (options1 contains the original dimensions, calculateSampleSizeForImageSize() calculates the sampleSize for smaller required Imagesize):
if (maximumImageSize < Runtime.getRuntime().maxMemory() / 4f) {
//enough memory free to reduce size
//reduce size
BitmapFactory.Options options2 = new BitmapFactory.Options();
options2.inSampleSize = calculateSampleSizeForImageSize(options1, maximumImageSize);
options2.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(is, null, options2);
try {
//write reduced size bitmap to picture file
os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
bitmap.recycle();
return 0;
} catch (IOException e) {
Log.d(TAG, "failed to fetch image", e);
throw new IOException(e);
} finally {
closeStream(os);
closeStream(is);
}
So is there a way to bypass the bitmap and directly write the input stream in reduced size to the output stream?
how to convert gif to base64?
here's what i try so far
public static String convertImageToBase64(String urlString, String type ){
String imageString = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
URL url = new URL(urlString);
BufferedImage img = ImageIO.read( url );
ImageIO.write(img, type, bos);
byte[] imageBytes = bos.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
imageString = encoder.encode(imageBytes);
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
return imageString;
}
code above is working, but it lost the animation
Most likely the class BufferedImage does not support images with animation. So the code opens and parses the image only to the first frame.
Instead try directly getting the bytes with URL.openStream and then just convert the downloaded bytes to base64.
Notice that this way you can't be sure that the downloaded file is actually an image since you are not opening it at all. This may or may not be needed.
You have to use
public String encodeToString(byte[] src)
of class BASE64.Encoder (from java 8)
I'm assuming what you want is a Base64 representation of the GIF file itself, not the ImageIO representation. If you have an older version of Java without built-in Base64 support, Apache Commons Codec will do the job. Read the file into a byte array using URL.openStream(), not with ImageIO, then call Base64.encodeBase64String. If you want the result URL encoded, call Base64.encodeBase64URLSafe instead.
If you actually want the ImageIO representation, then you're stuck with losing the animation.
I am not new to bitmaps nor new to java. I am trying to convert High resolution bitmaps to byte array in a loop. Please find code here:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream);
imageByteArray = stream.toByteArray();
When i am using the above approach I cam able to convert 5 images in 1 second. But I need it to be even faster. I tried ByteBuffer approach also like this:
Bitmap bmp = intent.getExtras().get("data");
int size = bmp.getRowBytes() * bmp.getHeight();
ByteBuffer b = ByteBuffer.allocate(size);
bmp.copyPixelsToBuffer(b);
byte[] bytes = new byte[size];
try {
b.get(bytes, 0, bytes.length);
} catch (BufferUnderflowException e) {
// always happens
}
But this is very slow (Slower then previous) :(
Please, can somebody give a faster method? Guide Me...
The first solution is the right one.
But two things can happen here:
The image is maybe not of JPEG type, so conversion occurs, which takes time
The image is compressed 50%, which takes time
That aside, if it's taking some time, I doubt it could go faster (being the right solution).
You must consider the fact that the speed of processing is tightly tied to the speed of the device you are testing on( since this is tagged android I'm presuming you're using a mobile device ).
You should take a look at android developer on how to handle large bitmaps effectively Android developers . Since processing 5 high resolution images per second is slow to you I can presume you are having some kind of gallery or previews? If that's the case you shouldn't handle the high resolution images and should indeed take a look at the link above.
Also as a side-note your second code can be optimised this way:
int bytes = bmp.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
bmp.copyPixelsToBuffer(buffer);
byte[] array = buffer.array();
Otherwise the most efficient way of copying bytes that I know is copy() taken from Commons-IO:
public static int copy(InputStream input, OutputStream output) throws IOException {
int n, count = 0;
byte[] buffer = new byte[4 * 1024];
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
you can try as follows
Bitmap bitmap = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap .compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
hope it may work good for you!!
check for line bitmap.compress(Bitmap.CompressFormat.JPEG, 50, stream); it may cause problem..as you are using JPEG format with rate 50.