i am trying to save some images to a web server. So what I doe is I convert my file in android to Base64 string, send it to my server and save the Base64 string in my database. When I need, I read the string from that database and decode it. But there is some problem with my image because the image is all grey.
This is my code to convert the image to base64:
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
int imageWidth = options.outWidth;
if (imageWidth > widthPixels) {
myBitmap.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
} else {
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
}
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
Then I send the string "encoded" to the server and save it in the database.
And this is part of my image saved in the server
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz
ODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj
for some reason there are some "\n" and I think that this is causing my problem. I have already tried to remove them with a replaceAll but didn't worked.
This is the output I get
This is my database structure:
I'm saving the encoded 64base image as a string in the column caled "imagemBase". Could the problem be in the type or in the codification that i'm using?
I using this methods an worked for me:
public static String encodeBitmap(Bitmap bitmap) {
if (bitmap != null) {
bitmap = resize(bitmap, 800, 800);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] byteArrayImage = baos.toByteArray();
return Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
}
return null;
}
public static Bitmap decodeBitmap(String encodedImage) {
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
}
public static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > ratioBitmap) {
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}
I have solved the problem. When I converted the image to base64 the string that I received hade some + cahrs in it and for some reason when I sent it to the server the + was replaced with a blank space. I needed to make a replace on the php file where I replaced all black spaces with the + character before saving the string in the database.
Related
Actually I'm trying to upload image into server with excellent quality .Currently I'm converting an array of bitmap to base64 string arrayList and then sending that to server by socket.io.But the problem is when I'm trying to increase the quality of bitmap the base64 conversion is taking a lot of time and it's quite inefficient .Even after taking so much time I'm getting low quality images in server .So can anyone please help me to fix this problem and increase the efficiency of upload ?
Code:
public ArrayList<String> convert(ArrayList<Bitmap> bitmap){
ArrayList<String> pics=new ArrayList<>();
for(int i=0;i<bitmap.size();i++){
pics.add(getStringImage(bitmap.get(i)));
}
return pics;
}
public static String getStringImage(Bitmap bmp){
//I've givent maxSize as 500
int outWidth;
int outHeight;
int inWidth = bmp.getWidth();
int inHeight = bmp.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap new_bitmap = Bitmap.createScaledBitmap(bmp, outWidth, outHeight, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
new_bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
// Sending to server
final JSONObject obji=new JSONObject();
try {
//bit is arraylist of bitmap
obji.put("uploads_username",username);
obji.put("uploads",convert(bit));
socket.emit("data",obji);
}catch(Exception e){
}
I have a function convert image to array byte (to save image into database sqlite). I have a problem, how to compress image and avoid out of memory error?
This is my code
Thanks in advance.
public byte[] ConverttoArrayByte(ImageView img)
{
try{
BitmapDrawable bitmapDrawable = (BitmapDrawable) img.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}catch (NullPointerException e){
Log.d("Tag", "Null");
e.printStackTrace();
}
return null;
}
You can try to use a small function that resizes the image:
public static Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
And call this function in this way Bitmap bitmap = GetAttachmentsUtils.getResizedBitmap(bitmap, maxSize); to get the resized bitmap.
Then you can compress it using bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); and perform the same operations you were doing previously.
I am converting image to Base64 to send it to the server, But the converted string is a huge string. If the converted image is ~100kb then the converted base64 value string is ~1mb...
My code...
protected String doInBackground(Void...arg0) {
Cursor cursor = mydb.getDat1();
//fetching the image location
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
for( int i=0 ; i< 1 ; i++ )
{
if( cursor.getColumnName(i) != null )
{
try
{
if( cursor.getString(i) != null )
{
//saving image to bitmap
Bitmap bitmap = BitmapFactory.decodeFile(cursor.getString(cursor.getColumnIndex(DBHelper.PHOTO)));
//converting it to base64
String en= encodeToBase64( resize(bitmap,1080,1920), Bitmap.CompressFormat.JPEG,100);
Log.d("base",en);
//inserting it to table pic
mydb.insertpic(cursor.getInt(1),en);
}
}
catch( Exception ignored)
{
}
}
}
cursor.moveToNext();
}
cursor.close();
mydb.updatebin();
return null;
}
private static Bitmap resize(Bitmap image, int maxWidth, int maxHeight) {
//for conerting images to lower resolution
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
int height = image.getHeight();
float ratioBitmap = (float) width / (float) height;
float ratioMax = (float) maxWidth / (float) maxHeight;
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = (int) ((float)maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float)maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
}
public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)
{
//converting image to base 64
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
How can i resolve this issue.?
This is the code:
Code to fetch image :
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
String encodedImage = encodeImage(selectedImage);
}
Try to compress and then convert(Bitmap)
private String encodeImage(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String encImage = Base64.encodeToString(b, Base64.DEFAULT);
return encImage;
}
My guess is that the encondeToBase64() is creating a large image file, since you resize the original image to 1920x1080 and convert to a jpeg with quality 100, and then the file then grows around 1.333... times in size when you convert to base64.
I am using Bitmap.createScaledBitmap() to Resize Image but as I have just checked it, It creates dull Image or say low quality image. Please let me know If I can get clear image.
I am using this method because I want to reduce photo size before uploading it to server.
My Code is as below :
final int maxSize = 250;
int outWidth;
int outHeight;
int inWidth = selectedImage.getWidth();
int inHeight = selectedImage.getHeight();
if (inWidth > inHeight) {
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(selectedImage,
outWidth, outHeight, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
String strBase64 = Base64.encodeToString(data, Base64.DEFAULT);
I want to make a thumbnail of an image.The image is in the resourse-drawable.Can anyone help me.
try this code
im=(ImageView)findViewById(R.id.imageView1);
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
//InputStream is=getAssets().open("apple-android-battle.jpg");
FileInputStream fis = new FileInputStream("/sdcard/apple.jpg");
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
Float width = new Float(imageBitmap.getWidth());
Float height = new Float(imageBitmap.getHeight());
Float ratio = width/height;
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, (int)(THUMBNAIL_SIZE * ratio), THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
im.setImageBitmap(imageBitmap);
}
catch(Exception ex) {
}
//read your drawables
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
Bitmap mThumbnail = scaleBitmap(BitmapFactory.decodeStream(is));
//now you can save the bitmap mThumbnail to SDcard
/*convert your image to an thumbnail view */
private static Bitmap scaleBitmap(Bitmap source) {
int maxSize = source.getWidth() > source.getHeight() ? source.getWidth() : source.getHeight();
return Bitmap.createScaledBitmap(source, source.getWidth() * 96 / maxSize, source.getHeight() * 96 / maxSize, true);
}