Today on the Internet to find a reference to the picture to compress to kb
Others do not want to say more, directly on the code
Call the following code after taking a photo
Bitmaputils.compressbitmap (Photopath, Photopath, 640); Compression
This is a tool class.
Package Com.continuouscamera;import Java.io.bytearrayoutputstream;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.app.activity;import Android.database.cursor;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.net.uri;import android.provider.mediastore.mediacolumns;/** * Tools For handler picture */public final class Bitmaputils {private static final String TAG = BitmapUtils.class.getSimpleNam E (); public static void Compressbitmap (String sourcepath, String targetpath, float maxSize) {bitmapfactory.options Opti ONS = new Bitmapfactory.options (); Options.injustdecodebounds = true; Bitmapfactory.decodefile (SourcePath, Options); Final float originalwidth = options.outwidth; Final float originalheight = options.outheight; float Convertedwidth; float Convertedheight; if (OriginalWidth > OriginalHeight) { Convertedwidth = maxSize; Convertedheight = Maxsize/originalwidth * originalheight; } else {convertedheight = maxSize; Convertedwidth = Maxsize/originalheight * originalwidth; } final float ratio = originalwidth/convertedwidth; options.insamplesize = (int) ratio; Options.injustdecodebounds = false; Bitmap Convertedbitmap = Bitmapfactory.decodefile (SourcePath, Options); Bytearrayoutputstream Bytearrayoutputstream = new Bytearrayoutputstream (); Convertedbitmap.compress (Bitmap.CompressFormat.JPEG, Bytearrayoutputstream); FileOutputStream FileOutputStream; try {fileoutputstream = new FileOutputStream (new File (TargetPath)); Fileoutputstream.write (Bytearrayoutputstream.tobytearray ()); Fileoutputstream.flush (); Fileoutputstream.close (); } catch (FileNotFoundException e) {e.printstacktrace (); } catch (Ioexception e) {e.printstacktrace (); }}/** * get picture physical path based on URI */public static String Getabsoluteimagepath (URI Uri, activity activity) { String[] proj = {Mediacolumns.data}; cursor cursor = activity.managedquery (URI, proj, NULL, NULL, NULL); int column_index = Cursor.getcolumnindexorthrow (Mediacolumns.data); Cursor.movetofirst (); Return cursor.getstring (Column_index); }/** * * @param path * @param maxSize * @return */public static BITMAP Decodebitmap (String Pat h, int maxSize) {bitmapfactory.options Options = new Bitmapfactory.options (); Options.injustdecodebounds = true; Bitmapfactory.decodefile (path, options); Final int originalwidth = Options.outwidth; Final int originalheight = options.outheight; int convertedwidth; int convertedheight; if (OriginalWidth > OriginalHeight) {convertedwidth = maxSize; Convertedheight = Maxsize/originalwidth * originalheight; } else {convertedheight = maxSize; Convertedwidth = Maxsize/originalheight * originalwidth; } options = new Bitmapfactory.options (); Options.injustdecodebounds = false; Options.insamplesize = computesamplesize (options, MaxSize, Convertedwidth * convertedheight); Bitmap convertedbitmap = bitmapfactory.decodefile (path, options); if (convertedbitmap! = null) {final int realwidth = Convertedbitmap.getwidth (); Final int realheight = Convertedbitmap.getheight (); } return Convertedbitmap; }/** * * @param path * @param maxWidth * @param maxheight * @return * * public static Bitmap Decodebitmap (String path, int maxWidth, int maxheight) {bitmapfactory.options Options = new Bitmapfactory.options (); Options.injustdecodebounds = true; Bitmapfactory.decodefile (Path, Options); Final int originalwidth = Options.outwidth; Final int originalheight = options.outheight; options = new Bitmapfactory.options (); Options.injustdecodebounds = false; Options.insamplesize = computesamplesize (options, MaxWidth, MaxWidth * maxheight); Bitmap convertedbitmap = bitmapfactory.decodefile (path, options); if (convertedbitmap! = null) {final int realwidth = Convertedbitmap.getwidth (); Final int realheight = Convertedbitmap.getheight (); } return Convertedbitmap; } private static int computeinitialsamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) { Double w = options.outwidth; Double h = options.outheight; int lowerbound = (Maxnumofpixels = =-1)? 1: (int) Math.ceil (math.sqrt (w * h/maxnumofpixels)); int upperbound = (Minsidelength = =-1)? Minsidelength: (int) math.min (Math.floor (w/minsidelength), MATH.FLoor (h/minsidelength)); if (Upperbound < lowerbound) {return lowerbound; } if ((Maxnumofpixels = =-1) && (minsidelength = =-1)) {return 1; } else if (minsidelength = =-1) {return lowerbound; } else {return upperbound; }} private static int computesamplesize (bitmapfactory.options Options, int minsidelength, int maxnumofpixels) { int initialsize = computeinitialsamplesize (options, Minsidelength, maxnumofpixels); int roundedsize; if (initialsize <= 8) {roundedsize = 1; while (Roundedsize < initialsize) {roundedsize <<= 1; }} else {roundedsize = (initialsize + 7)/8 * 8; } return roundedsize; }/** * Generates a 8-bit 16-binary cache factor: 8-bit hash code for the rule, less than 0 * @param string * @return */public static string Toregularhas Hcode (String string) {final String HEXHASHCOde = integer.tohexstring (String.hashcode ()); Final StringBuilder StringBuilder = new StringBuilder (Hexhashcode); while (Stringbuilder.length () < 8) {Stringbuilder.insert (0, ' 0 '); } return stringbuilder.tostring (); }}
Android camera ensures that images are not distorted and compressed