문제

My app is currently sending images from an Android device to a PHP script by converting the image into a bit array and then converting to base64. The base64 string is then sent in a HTTP request.

The problem is that is the image is big (like the ones taken from android camera) then the transfer fails. What i want to do is change the image size before it goes through the conversion process.

How can i do this? I've tried to google it but have had no luck so far.

도움이 되었습니까?

해결책

If your image size is big then you have to need first scale in to small size then encode this by base64 class then you send this on your server.

For scale your image read this http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application

or other post

다른 팁

Use jpeg compression!

('Cause I'm not sure how sending up a base64 encoded byte array is going to save you any space.)

May I jump in and assume you've got a stage where you've converted your image into an array of pixels instead? If not, I'll assume there is no reason why the obvious conversion from bytes to integers representing pixels applies. Then we'll convert it to a compressed jpeg.

final int[] pixels = yourpixels;

You'll also need width and height:

final int width = theWidth; etc...

Next, get hold of your output stream in your client:

final HttpURLConnection connection = doWhateverYouDoToOpenYourConnection();
final OutputStream httpOutputStream = connection.getOutputStream();

Now the crucial step is to use the compression methods of Android's bitmap library to stream the compressed image onto the http output stream:

final Bitmap androidBitmap = Bitmap.createBitmap(pixels, width, height,Config.ARGB_8888);
androidBitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, YOUR_QUALITY_INT, outputStream);

Start with something like YOUR_QUALITY_INT = 85 to see significant improvement in image size without much visible deformation.

If this fails, create a scaled bitmap from a scale matrix: documentation here. This reduces the width and height of your bitmap on creation, which obviously reduces request size.

Hope this helps.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top