许多应用程序允许共享从图库中挑选的图像.
他们上传原始图片文件吗?这是1-3 MB?或者他们处理?
在任何情况下,我如何从文件路径中获取图像,通过降低分辨率来减小其大小,并将其保存在其他地方并尝试上传?
我试过了:
Bitmap photo = decodeSampledBitmapFromfile(filePath, DESIRED_WIDTH, DESIRED_HEIGHT);fileOutputStream out = new fileOutputStream(filePath);photo.compress(Bitmap.CompressFormat.JPEG, 100, out);public static Bitmap decodeSampledBitmapFromfile(String path, int reqWIDth, int reqHeight) { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodefile(path, options); final int height = options.outHeight; final int wIDth = options.outWIDth; options.inPreferredConfig = Bitmap.Config.ARGB_8888; int inSampleSize = 1; if (height > reqHeight) { inSampleSize = Math.round((float) height / (float) reqHeight); } int expecteDWIDth = wIDth / inSampleSize; if (expecteDWIDth > reqWIDth) { inSampleSize = Math.round((float) wIDth / (float) reqWIDth); } options.inSampleSize = inSampleSize; options.inJustDecodeBounds = false; return BitmapFactory.decodefile(path, options);}
但这是他们正确的方法吗?因为我看到的答案表明压缩 *** 作花费了相当多的时间here
解决方法:
这很有效试试这个
private String decodefile(String path,int DESIREDWIDTH, int DESIREDHEIGHT) { String strMyImagePath = null; Bitmap scaledBitmap = null; try { // Part 1: Decode image Bitmap unscaledBitmap = ScalingUtilitIEs.decodefile(path, DESIREDWIDTH, DESIREDHEIGHT, ScalingLogic.FIT); if (!(unscaledBitmap.getWIDth() <= DESIREDWIDTH && unscaledBitmap.getHeight() <= DESIREDHEIGHT)) { // Part 2: Scale image scaledBitmap = ScalingUtilitIEs.createScaledBitmap(unscaledBitmap, DESIREDWIDTH, DESIREDHEIGHT, ScalingLogic.FIT); } else { unscaledBitmap.recycle(); return path; } // Store to tmp file String extr = Environment.getExternalStorageDirectory().toString(); file mFolder = new file(extr + "/TMMFolDER"); if (!mFolder.exists()) { mFolder.mkdir(); } String s = "tmp.png"; file f = new file(mFolder.getabsolutePath(), s); strMyImagePath = f.getabsolutePath(); fileOutputStream fos = null; try { fos = new fileOutputStream(f); scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 75, fos); fos.flush(); fos.close(); } catch (fileNotFoundException e) { e.printstacktrace(); } catch (Exception e) { e.printstacktrace(); } scaledBitmap.recycle(); } catch (Throwable e) { } if (strMyImagePath == null) { return path; } return strMyImagePath; }
ScalingUtilitIEs.java
import androID.content.res.Resources;import androID.graphics.Bitmap;import androID.graphics.Bitmap.Config;import androID.graphics.BitmapFactory;import androID.graphics.BitmapFactory.Options;import androID.graphics.Canvas;import androID.graphics.Paint;import androID.graphics.Rect;/** * Class containing static utility methods for bitmap deCoding and scaling * * @author */public class ScalingUtilitIEs { /** * Utility function for deCoding an image resource. The decoded bitmap will * be optimized for further scaling to the requested destination dimensions * and scaling logic. * * @param res The resources object containing the image data * @param resID The resource ID of the image data * @param dstWIDth WIDth of destination area * @param dstHeight Height of destination area * @param scalingLogic Logic to use to avoID image stretching * @return Decoded bitmap */ public static Bitmap decodeResource(Resources res, int resID, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(res, resID, options); options.inJustDecodeBounds = false; options.inSampleSize = calculateSampleSize(options.outWIDth, options.outHeight, dstWIDth, dstHeight, scalingLogic); Bitmap unscaledBitmap = BitmapFactory.decodeResource(res, resID, options); return unscaledBitmap; } public static Bitmap decodefile(String path, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodefile(path, options); options.inJustDecodeBounds = false; options.inSampleSize = calculateSampleSize(options.outWIDth, options.outHeight, dstWIDth, dstHeight, scalingLogic); Bitmap unscaledBitmap = BitmapFactory.decodefile(path, options); return unscaledBitmap; } /** * Utility function for creating a scaled version of an existing bitmap * * @param unscaledBitmap Bitmap to scale * @param dstWIDth Wanted wIDth of destination bitmap * @param dstHeight Wanted height of destination bitmap * @param scalingLogic Logic to use to avoID image stretching * @return New scaled bitmap object */ public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { Rect srcRect = calculateSrcRect(unscaledBitmap.getWIDth(), unscaledBitmap.getHeight(), dstWIDth, dstHeight, scalingLogic); Rect dstRect = calculateDstRect(unscaledBitmap.getWIDth(), unscaledBitmap.getHeight(), dstWIDth, dstHeight, scalingLogic); Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.wIDth(), dstRect.height(), Config.ARGB_8888); Canvas canvas = new Canvas(scaledBitmap); canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG)); return scaledBitmap; } /** * ScalingLogic defines how scaling should be carrIEd out if source and * destination image has different aspect ratio. * * CROP: Scales the image the minimum amount while making sure that at least * one of the two dimensions fit insIDe the requested destination area. * Parts of the source image will be cropped to realize this. * * FIT: Scales the image the minimum amount while making sure both * dimensions fit insIDe the requested destination area. The resulting * destination dimensions might be adjusted to a smaller size than * requested. */ public static enum ScalingLogic { CROP, FIT } /** * Calculate optimal down-sampling factor given the dimensions of a source * image, the dimensions of a destination area and a scaling logic. * * @param srcWIDth WIDth of source image * @param srcHeight Height of source image * @param dstWIDth WIDth of destination area * @param dstHeight Height of destination area * @param scalingLogic Logic to use to avoID image stretching * @return Optimal down scaling sample size for deCoding */ public static int calculateSampleSize(int srcWIDth, int srcHeight, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { if (scalingLogic == ScalingLogic.FIT) { final float srcAspect = (float)srcWIDth / (float)srcHeight; final float dstAspect = (float)dstWIDth / (float)dstHeight; if (srcAspect > dstAspect) { return srcWIDth / dstWIDth; } else { return srcHeight / dstHeight; } } else { final float srcAspect = (float)srcWIDth / (float)srcHeight; final float dstAspect = (float)dstWIDth / (float)dstHeight; if (srcAspect > dstAspect) { return srcHeight / dstHeight; } else { return srcWIDth / dstWIDth; } } } /** * Calculates source rectangle for scaling bitmap * * @param srcWIDth WIDth of source image * @param srcHeight Height of source image * @param dstWIDth WIDth of destination area * @param dstHeight Height of destination area * @param scalingLogic Logic to use to avoID image stretching * @return Optimal source rectangle */ public static Rect calculateSrcRect(int srcWIDth, int srcHeight, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { if (scalingLogic == ScalingLogic.CROP) { final float srcAspect = (float)srcWIDth / (float)srcHeight; final float dstAspect = (float)dstWIDth / (float)dstHeight; if (srcAspect > dstAspect) { final int srcRectWIDth = (int)(srcHeight * dstAspect); final int srcRectleft = (srcWIDth - srcRectWIDth) / 2; return new Rect(srcRectleft, 0, srcRectleft + srcRectWIDth, srcHeight); } else { final int srcRectHeight = (int)(srcWIDth / dstAspect); final int scrRecttop = (int)(srcHeight - srcRectHeight) / 2; return new Rect(0, scrRecttop, srcWIDth, scrRecttop + srcRectHeight); } } else { return new Rect(0, 0, srcWIDth, srcHeight); } } /** * Calculates destination rectangle for scaling bitmap * * @param srcWIDth WIDth of source image * @param srcHeight Height of source image * @param dstWIDth WIDth of destination area * @param dstHeight Height of destination area * @param scalingLogic Logic to use to avoID image stretching * @return Optimal destination rectangle */ public static Rect calculateDstRect(int srcWIDth, int srcHeight, int dstWIDth, int dstHeight, ScalingLogic scalingLogic) { if (scalingLogic == ScalingLogic.FIT) { final float srcAspect = (float)srcWIDth / (float)srcHeight; final float dstAspect = (float)dstWIDth / (float)dstHeight; if (srcAspect > dstAspect) { return new Rect(0, 0, dstWIDth, (int)(dstWIDth / srcAspect)); } else { return new Rect(0, 0, (int)(dstHeight * srcAspect), dstHeight); } } else { return new Rect(0, 0, dstWIDth, dstHeight); } }}
总结 以上是内存溢出为你收集整理的android – 如何在上传到服务器之前减少图像文件大小全部内容,希望文章能够帮你解决android – 如何在上传到服务器之前减少图像文件大小所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)