I had a problem where some of my images are loaded inside imageview rotated. I solved the problem by loading an image using Glide.
The image is loaded inside 100 x 100 imageview. Can I retrieve the image which is stored inside that imageview as 100 x 100 and upload it to the server instead of sending the original image?
Yes you can
public byte[] getImageFromImageView(ImageView imageView,String filePath) {
Drawable imageDrawable = imageView.getDrawable();
Bitmap imageBitmap = ((BitmapDrawable)imageDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
return byteArray;
}
Then upload file to server or you can create RequestBodyand send it to server if you using Retrofit.
Related
How to compress jpg/bmp files which I can store in the memory then when needed decompress those images and show to users without losing too much of image quality? How to do the compress and decompress, any guidance/ link would be helpful.
Thank you
create image thumbnails
'byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}`
if u need for thumbNail then use
ThumbNailUtils.extractThumbnail(Bitmap source,int width,int height)
it show thumbnail from bitmap and when u want to show original bitmap then show bitmap
.
use wisely bitmap object cause it take more memory at runtime.
I have an app that takes a picture, assigns it to an imageview and then uploads it to a parse server.
The problem is that I need the images to be 200 x 200 px before upload.
Is there a way to do this programatically?
this is my function so far:
public void Myfunction (){
//image
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap bitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, baos);
byte[] imageInByte = baos.toByteArray();
ParseFile file = new ParseFile("myimage.jpg",imageInByte);
Could anyone help me please?
To scale bitmap down before compress, add
bitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false));
If your bitmap is not square, you probably want to crop it before scaling
I have a camera sending frames to a SurfaceView. I would like to get these frames out of the surface view and send them elsewhere. In their final form, the images must be in JPEG format. To accomplish this currently, I am creating a YUV image from the byte[] and then calling compressToJpeg. However, when I invoke compressToJpeg on every frame rather than doing nothing but displaying it, my FPS goes from ~30 to ~4. I commented out the other lines and this function appears to be the culprit.
public void onNewRawImage(byte[] data, Size size) {
// Convert to JPG
YuvImage yuvimage=new YuvImage(data,
ImageFormat.NV21, size.width, size.height, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvimage.compressToJpeg(new Rect(0, 0, yuvimage.getWidth(),
yuvimage.getHeight()), 80, baos);
byte[] jdata = baos.toByteArray();
// Convert to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(jdata, 0, jdata.length);
}
Is it possible to start in the JPEG format rather than having to convert to it? I am hoping I am making a mistake somewhere. Any help is greatly appreciated, thank you.
I am trying to build a function for the android phone that will open the CameraAPI and then send the captured picture to a new activity.
In this activity i need it to show a preview for the image and then save.
How could i go about doing this without having to save the image to the SD card?
You can store the image into a bitmap and then convert it to byte array and then transfer to other activity through this code below
Bitmap bitmapPicture= "PICTURE FROM CAMERA";
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmapPicture.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Then Pass Through Intent
Intent imagepass = new Intent(Cam.this,Preview.class);
imagepass.putExtra("imagepass", bytes.toByteArray());
startActivity(imagepass);
In Other Activity we have to recieve the byte array and convert to bitmap and display in imageview through
Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("imagepass");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView iv=(ImageView) findViewById(R.id.imgvw2);
iv.setImageBitmap(bmp);
You will have to use custom camera control Hope This Helps.
I am creating an application over Android where I need to manipulate my JPG files. I am not getting much of header information for JPG format so for that I am converting it to Bitmap, manipulated the pixel values in bitmap and then again convert it back to JPG.
Here what problem I am facing is- after manipulating only some pixels of bitmap and
converting it back to JPG, I do not get the same set of pixels I got earlier (for those pixels which I did not manipulate). I am getting the same image as the original in the new image. But when I check new image pixels values for decoding, the untouched pixels are different...
File imagefile = new File(filepath);
FileInputStream fis = new FileInputStream(imagefile);
Bitmap bi = BitmapFactory.decodeStream(fis);
int intArray[];
bi=bi.copy(Bitmap.Config.ARGB_8888,true);
intArray = new int[bi.getWidth()*bi.getHeight()];
bi.getPixels(intArray, 0, bi.getWidth(), 0, 0, bi.getWidth(), bi.getHeight());
int newArray[] = encodeImage(msgbytes,intArray,mbytes); // method where i am manipulating my pixel values
// converting the bitmap data back to JPG file
bi = Bitmap.createBitmap(newArray, bi.getWidth(), bi.getHeight(), Bitmap.Config.ARGB_8888);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bi.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
Bitmap bitmapimage = BitmapFactory.decodeByteArray(data, 0, data.length);
String filepath = "/sdcard/image/new2.jpg";
File imagefile = new File(filepath);
FileOutputStream fos = new FileOutputStream(imagefile);
bitmapimage.compress(CompressFormat.JPEG, 100, fos);
Help me if I am wrong somewhere or whether I should use some other method to manipulate JPG pixel values...
JPEG is an image format that is usually based on lossy compression. That means that some information that is not important for the human eye is thrown away to further shrink the file size. Try to save your image as a PNG (a lossless format).
Be careful with using
Bitmap bi = BitmapFactory.decodeStream(fis);
bi = bi.copy(Bitmap.Config.ARGB_8888, true);
At the point where you have the first bi you may have already lost a lot of information, instead try using BitmapFactory.Options to force 8888 (which is the default too):
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inDither = false;
Bitmap bi = BitmapFactory.decodeStream(fis, options);
If you stay with copy you should still recycle() the one that you throw away.