I want to write my own code for compressing image before I upload it to server.
I have seen many posts on SO like this and there are many other posts also but every where the code is written. It is not explained anywhere. I am not able to understand from the given code.
I just want to know about the approach to compress image so that I can start writing code by myself.
I am not looking for the code. Only the steps that one person needs to follow to compress an image. (Like an algorithm is written before writing program)
I think Chetan Joshi's answer is the closest so far. But to explain a bit further,
"Compressing" an image and "scaling" (resizing) an image are different. Since you merely want to reduce the bandwidth (kb/Mb) over the network when you upload to the server, you are most likely talking about "scaling" since that will make the most difference to the size of the image.
Note, as in other answers Android, you will need to work with Bitmaps. You need to use the API calls associated with Bitmaps to do what you want.
If you allow your app to take a photo using the camera or pick from the gallery, you get an Uri which allows you to open an InputStream like this:
InputStream is = getContext().getContentResolver().openInputStream(photoUri);
That's the first step. The next part is the pseudocode where you want something like this:
InputStreamof the photo/selected picture. This is just a stream of bytes that represent the image.
Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter);. You'll have to decide on a destination width and destination height you want yourself. See the next set of pseudocode.
imageFormat.getBitmapCompressFormat(), 100, output)where output is an instance of
output.toByteArray();to get the bytes.
Note about how to choose the destination width and height to scale to:
Altogether the high level process is something like this: