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.
Related
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);
Sorry for my english. I get image from camera, but i have bad quality image from camera. Bellow my code:
Bitmap thumbnail = (Bitmap) imageReturnedIntent.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.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();
}
image.setImageBitmap(thumbnail);
I get image from camera, but i have bad quality image from camera.
Apparently, you are not including EXTRA_OUTPUT in your Intent, and therefore you are only getting the thumbnail. Quoting the documentation for ACTION_IMAGE_CAPTURE:
The caller may pass an extra EXTRA_OUTPUT to control where this image will be written. If the EXTRA_OUTPUT is not present, then a small sized image is returned as a Bitmap object in the extra field. This is useful for applications that only need a small image. If the EXTRA_OUTPUT is present, then the full-sized image will be written to the Uri value of EXTRA_OUTPUT.
when a user is taking a photo I want to save it to background as a parse file as follows:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
try {
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
filePic = new ParseFile(userName + ".png", byteArray);
filePic.saveInBackground();
This code works, but the problem is that the image loses its quality even though its set to 100.
Can anyone help with a solution on how to compress the bitmap without losing the quality? or maybe saving it without compressing?
I have multiple activities that each one apply some visual effects on screen then save and send it's uri to another activity,but after each process(getDrawingCache + save + send)some noises appear on image,this is my code:
FrameLayout frame1=(FrameLayout) findViewById(R.id.finalview_new);
frame1.setDrawingCacheEnabled(true);
frame1.buildDrawingCache();
Bitmap bitmap=frame1.getDrawingCache();
String fileName = "myImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes =
new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo =
openFileOutput(fileName, aftertext.this.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
fileName = null;
}
ed.setVisibility(View.VISIBLE);
onCreate(null);
This is the culprit
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
JPEG is a lossy format. Use the BitmapFormat.PNG option instead.
I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.