调整文件的图像大小

我正在上传图像到服务器,在这之前,我想调整图像尺寸。 我用这样的URI获取图像:

 Constants.currImageURI = data.getData(); 

这是呼吁上传图像:

 String response = uploadUserPhoto(new File(getRealPathFromURI(Constants.currImageURI))); public String uploadUserPhoto(File image) { DefaultHttpClient mHttpClient; HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); mHttpClient = new DefaultHttpClient(params); try { HttpPost httppost = new HttpPost("http://myurl/mobile/image"); MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); multipartEntity.addPart("userID", new StringBody(Constants.userID)); multipartEntity.addPart("uniqueMobileID", new StringBody(Constants.uniqueMobileID)); multipartEntity.addPart("userfile", new FileBody(image, "mobileimage.jpg", "image/jpeg", "UTF-8")); httppost.setEntity(multipartEntity); HttpResponse response = mHttpClient.execute(httppost); String responseBody = EntityUtils.toString(response.getEntity()); Log.d(TAG, "response: " + responseBody); return responseBody; } catch (Exception e) { Log.d(TAG, e.getMessage()); } return ""; } 

有没有办法根据像素尺寸调整文件的大小?

谢谢。

Solutions Collecting From Web of "调整文件的图像大小"

这是取自ThinkAndroid在这个url: http ://thinkandroid.wordpress.com/2009/12/25/resizing-a-bitmap/

我会研究从资源创build一个位图或绘图的可能性,如果你想改变它的大小使用下面的代码。

 public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; // Create a matrix for the manipulation Matrix matrix = new Matrix(); // Resize the bit map matrix.postScale(scaleWidth, scaleHeight); // Recreate the new Bitmap Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); return resizedBitmap; } 

编辑:如其他评论build议Bitmap.createScaledBitmap应该用于更好的质量,当resize。

像其他人一样使用Bitmap.createScaledBitmap

但是,这个function不是很聪明。 如果你缩小到小于50%的大小,你可能会得到这个:

在这里输入图像说明

而不是这个:

在这里输入图像说明

你看到第一张图片的抗锯齿效果不好吗? createScaledBitmap会得到这个结果。

原因是像素过滤,如果缩放到<50%,某些像素将完全从源代码跳过。

要获得第二个质量的结果,如果比所需结果大2倍以上,则需要将位图分辨率减半,最后再调用createScaledBitmap。

还有更多的办法来减less(或四分之一,或提高)图像。 如果在内存中有位图,则recursion调用Bitmap.createScaledBitmap以将图像减半。

如果你从JPG文件加载图像,实现更快:你使用BitmapFactory.decodeFile和设置选项参数正确,主要是字段inSampleSize ,它控制加载的图像subambling,利用JPEG的特点。

许多提供图像缩略图的应用程序一味使用Bitmap.createScaledBitmap,而缩略图只是丑陋的。 要聪明,并使用适当的图像下采样。

如何调整位图大小:

 Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter) 

看看Google build议的做法 ( @Pointer Nullbuild议 ):

 public int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { final int halfHeight = height / 2; final int halfWidth = width / 2; // Calculate the largest inSampleSize value that is a power of 2 and keeps both // height and width larger than the requested height and width. while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) { inSampleSize *= 2; } } return inSampleSize; } 

我打电话给上面调整一个大的图像:

 // Check if source and destination exist // Check if we have read/write permissions int desired_width = 200; int desired_height = 200; BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(SOME_PATH_TO_LARGE_IMAGE, options); options.inSampleSize = calculateInSampleSize(options, desired_width, desired_height); options.inJustDecodeBounds = false; Bitmap smaller_bm = BitmapFactory.decodeFile(src_path, options); FileOutputStream fOut; try { File small_picture = new File(SOME_PATH_STRING); fOut = new FileOutputStream(small_picture); // 0 = small/low quality, 100 = large/high quality smaller_bm.compress(Bitmap.CompressFormat.JPEG, 50, fOut); fOut.flush(); fOut.close(); smaller_bm.recycle(); } catch (Exception e) { Log.e(LOG_TAG, "Failed to save/resize image due to: " + e.toString()); }