本帖原创,转发请标记出处。实在是本人一些肤浅的经验之谈,大神可绕行。另外如有不足之处或者可以优化的地方
欢迎指出,万分感谢。只为相互学习和进步。如果能对您有所帮助或者启发,便是我最开心的事。
第二部分:图片的压缩,缩略图的处理
继续上次说道的 从拍照或者相册中拿到了图片的地址或者URL,想上传到服务器,应该使之成为文件File,然后进行上传,上传服务器应进行压缩。否则服务器的压力和上传速度都会下降。当然如果有特殊的需求,可根据情况而定。
1.图片地址path 和 文件File 以及Bitmap之间的相互转化方法可自行查找。
public static void getBitmapForImgResourse(Context mContext, int imgId, ImageView mImageView) throws IOException { InputStream is = mContext.getResources().openRawResource(imgId); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = false; options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable = true; options.inInputShareable = true; options.inSampleSize = 1; btp = BitmapFactory.decodeStream(is, null, options); mImageView.setImageBitmap(btp); // btp.recycle(); is.close(); }
/** * 得到本地或者网络上的bitmap url - 网络或者本地图片的绝对路径,比如: * * A.网络路径: url="http://blog.foreverlove.us/girl2.png" ; * * B.本地路径:url="file://mnt/sdcard/photo/image.png"; * * C.支持的图片格式 ,png, jpg,bmp,gif等等 * * @param url * @return */ public static Bitmap GetLocalOrNetBitmap(String url) { Bitmap bitmap = null; InputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new URL(url).openStream(), 10*1024); final ByteArrayOutputStream dataStream = new ByteArrayOutputStream(); out = new BufferedOutputStream(dataStream, 10*1024); copy(in, out); out.flush(); byte[] data = dataStream.toByteArray(); bitmap = BitmapFactory.decodeByteArray(data, 0, data.length); data = null; return bitmap; } catch (IOException e) { e.printStackTrace(); return null; } }
。。。
2.图片的压缩
先说从体积上的压缩。如:3000K 压缩到300K
/** * 图像压缩算法 * * @param bmp 图片Bitmap * @param path 路径 * @param quality 压缩比例 100 最为清晰 压缩力度最小 0压缩力度最大 * @return 返回压缩之后的文件 */ public static File compressBmpToFile(Bitmap bmp, String path, int quality) { File file = new File(path); Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG; OutputStream stream = null; try { stream = new FileOutputStream(path); bmp.compress(format, quality, stream); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return file; }
当然如果你局的清晰度100 还是不行的话 写100以上也是不起作用的 可以参看源码
/** * Write a compressed version of the bitmap to the specified outputstream. * If this returns true, the bitmap can be reconstructed by passing a * corresponding inputstream to BitmapFactory.decodeStream(). Note: not * all Formats support all bitmap configs directly, so it is possible that * the returned bitmap from BitmapFactory could be in a different bitdepth, * and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque * pixels). * * @param format The format of the compressed image * @param quality Hint to the compressor, 0-100. 0 meaning compress for * small size, 100 meaning compress for max quality. Some * formats, like PNG which is lossless, will ignore the * quality setting * @param stream The outputstream to write the compressed data. * @return true if successfully compressed to the specified stream. */ public boolean compress(CompressFormat format, int quality, OutputStream stream) { checkRecycled("Can't compress a recycled bitmap"); // do explicit check before calling the native method if (stream == null) { throw new NullPointerException(); } if (quality < 0 || quality > 100) { throw new IllegalArgumentException("quality must be 0..100"); 《《《《区间0-100 } Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, "Bitmap.compress"); boolean result = nativeCompress(mNativeBitmap, format.nativeInt, quality, stream, new byte[WORKING_COMPRESS_STORAGE]); Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); return result; }
缩略图的处理 退出系统以后应该删除临时文件 import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import android.graphics.Bitmap; import android.graphics.BitmapFactory; public class Bimp { public static int max = 0; public static boolean act_bool = true; public static List<Bitmap> bmp = new ArrayList<Bitmap>(); //图片sd地址 上传服务器时把图片调用下面方法压缩后 保存到临时文件夹 图片压缩后小于100KB,失真度不明显 public static List<String> drr = new ArrayList<String>(); public static Bitmap revitionImageSize(String path) throws IOException { BufferedInputStream in = new BufferedInputStream(new FileInputStream( new File(path))); BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, options); in.close(); int i = 0; Bitmap bitmap = null; while (true) { if ((options.outWidth >> i <= 1000) && (options.outHeight >> i <= 1000)) { in = new BufferedInputStream( new FileInputStream(new File(path))); options.inSampleSize = (int) Math.pow(2.0D, i); options.inJustDecodeBounds = false; bitmap = BitmapFactory.decodeStream(in, null, options); break; } i += 1; } return bitmap; } }删除临时文件可以在生命周期销毁方法里写明清空。此略