Converting image file into base 64 taking more time - android

I am using the below code to convert image file into base 64 but it's taking more time to convert.
Bitmap bm = BitmapFactory.decodeFile("file");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String base64= Base64.encodeToString(b,Base64.DEFAULT);
If any one can help please do that!

check the size of the image you are converting .
now a days image size can be very high . so compressing the image before base64 encode will be better. this may reduce encoding time.

FileOutputStream fo;
try {
name_of_imagefile.createNewFile();
fo = new FileOutputStream(name_of_imagefile);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(thumbnail, 350, 350, false);
ByteArrayOutputStream bytes2 = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes2);
String encoded = "data:image/jpeg;base64,"+Base64.encodeToString( bytes2 .toByteArray(), Base64.DEFAULT);

Related

After Uploading photo on server from android Application, it lost his quality

While I select the image from the gallery and shows it in ImageView. The image quality is all right. But, uploading an image on the server, it lost quality and become a blur. I obtain the image from the camera by this code.
private void onCaptureImageResult(Intent data) {
bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File destination = new File(
Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg"
);
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
Then, I did this work-
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte,Base64.DEFAULT);
}
and used this function to compress my selected photo. But, it makes the loss of that image quality and image become a blur on the server. Why am I facing this problem?
You could use the .png format as it's lossless and doesn't reduce the image quality. On the other hand the .jpeg format is just the opposite of this.
private String imageToString(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] imgByte=byteArrayOutputStream.toByteArray();
return Base64.encodeToString(imgByte, Base64.DEFAULT);
}
You did not show the intent to start a Camera app.
But you did it in such a way that you only got a thumbnail of the picture taken.
Change the intent. Add an uri where the camera app can save the full picture.
There are 783 examples of such an intent on stackoverflow and even more on the internet.

getting null point exception while converting file to Base64

I'm going to convert file to base64 so I send file and convert it to bitmap and when i want to compress it, it give me error null point exception
this is what everything that i did.
public static String getFileToByte(String path){
Bitmap bm = null;
ByteArrayOutputStream baos = null;
byte[] b = null;
String encodeString = null;
try{
bm = BitmapFactory.decodeFile(path);
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
b = baos.toByteArray();
encodeString = Base64.encodeToString(b, Base64.DEFAULT);
}catch (Exception e){
e.printStackTrace();
}
return encodeString;
}
I got error on this error:
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
and pass:
getFileToByte(image.getAbsolutePath());
Do not convert the file to a bitmap first. Your bitmap is null as there is not enough memory to construct a bitmap for that image file with big resolution.
Instead you should directly base64 encode the bytes of the file.
Then your code is the same for all kind of files too.

byte[] to png file in Android

Hi I am working on application which gets .png files in byte stream from server. When I got it I try to make from it bmp and later convert it to .png file, but the following method ( Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length); ) returns me null.
Here is my code:
byte[] result
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
try {
File filename = new File(imageUri.getPath()+name);
File parentFile = new File(imageUri.getPath());
parentFile.mkdirs();
FileOutputStream out = new FileOutputStream(filename);
img.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (FileNotFoundException e) {
Log.e("imageDownloaded", e.toString());
} catch (Exception e) {
Log.e("imageDownloaded", e.toString());
}
but the img Bitmap is always, null. I uploaded the image as multi part data and it was png file parsed to byte array, but now when i want to retrieve it i get this ugly null. Thanks for any help.

How can I import an image to my PDF using itext

I already create table and some extra information as a PDF. But I couldn't import image to my PDF whatever I did. I have my_thumb.png file in assets folder.
in my MainActivity class
try {
inputStream = MainActivity.this.getAssets().open("my_thumb.png");
Bitmap bmp = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
model.setThumbStream(stream);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
in my CreatePDF class
Image thumb = Image.getInstance(Model.getThumbStream().toByteArray());
companyLogo.setAbsolutePosition(document.leftMargin(),121);
companyLogo.scalePercent(25);
document.add(thumb);
and the problem is
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
this line returns with null.
What is the point that I missing?
Thanks in advance.
You first get the png to drawable
Drawable d = Drawable.createFromStream(getAssets().open("my_thumb.png"), null);
then convert drawable to bitmap and get the byte array :)
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();

Getting Image from drawable and adding to PDF using iText

I want to add image to android PDF using iText. I want to achieve this without saving image to SDCard first. I put my image into res/drawable folder but proving the image path doesn’t work and it throws FileNotFound Exception. My path is like this:
String path = “res/drawable/myImage.png”
Image image = Image.getInstance(path);
document.add(image);
Now please suggest me a solution how I will add correct file path to getInstance(…) method. Thanks
Of course it'll not work at that way.
move your image to assets folder to access it with getassets() method
// load image
try {
// get input stream
InputStream ims = getAssets().open("myImage.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
}
catch(IOException ex)
{
return;
}
I found a solution for your issue. If you want to get image from your drawable folder and put it into a PDF file using iText use this code:
try {
document.open();
Drawable d = getResources().getDrawable(R.drawable.myImage);
BitmapDrawable bitDw = ((BitmapDrawable) d);
Bitmap bmp = bitDw.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
document.close();
} catch (Exception e) {
e.printStackTrace();
}
Here is the code to add image to PDF using iText, if the image is dynamic (i.e), if the image cannot be added to asset folder at compile time,
public void addImage(Document document,ImageView ivPhoto) throws DocumentException {
try {
BitmapDrawable drawable = (BitmapDrawable) ivPhoto.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageInByte = stream.toByteArray();
Image image = Image.getInstance(imageInByte);
document.add(image);
}
catch(IOException ex)
{
return;
}
}
Here is my code, To set Image on particular position
move your image to assets folder to get image by getassets() method.
Hope this will help you!
try {
InputStream ims = getAssets().open("header1.png");
Bitmap bmp = BitmapFactory.decodeStream(ims);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image image = Image.getInstance(stream.toByteArray());
image.setAbsolutePosition(10f,750f);
image.scaleToFit(850,78);
document.add(image);
}
catch(IOException ex)
{
ex.printStackTrace();
return;
}
try {
FileInputStream in = new FileInputStream("input file uri");
PdfReader pdfReader = new PdfReader(in);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("output file uri"));
PdfContentByte content = pdfStamper.getOverContent(1);
Image deliverImg = Image.getInstance("image URI");
deliverImg.setAbsolutePosition(420f, 100f);
content.addImage(deliverImg);
pdfStamper.close();
} catch (DocumentException de) {
Log.e("PDFCreator", "DocumentException:" + de);
} catch (IOException e) {
Log.e("PDFCreator", "ioException:" + e);
}

Categories

Resources