Author: Ching
Original address: http://blog.csdn.net/qingdujun/article/details/39480117
The size of the photo taken by the general camera is around 3-4m, where you need to compress the resulting photos by uploading them to the server function.
Online search for a lot of information, know that you can use: Insamplesize set the scale of the picture.
However, it is important to note:
1) Injustdecodebounds = true; Need to be set to true first, indicating that only the information of the picture is obtained. If at this time test bitmap will find bitmap==null;
2) If you need to load the picture, you must reset Injustdecodebounds = false;
One, to achieve picture compression (see others on the Internet, their own slightly modified a bit):
Compressed picture size public Bitmap compressbysize (String pathName, int targetwidth, int targetheight) {Bitma Pfactory.options opts = new Bitmapfactory.options (); Opts.injustdecodebounds = true;//do not really parse the picture, just get the image of the head information, including the width of higher; Bitmap Bitmap = Bitmapfactory.decodefile (PathName, opts); Get the width and height of the picture; float imgwidth = opts.outwidth; float imgheight = opts.outheight; Calculates the ratio of the width of the picture, the height to the target width, the height, and the smallest integer greater than or equal to the ratio; int widthRatio = (int) Math.ceil (ImgWidth/(float) targetwidth); int heightratio = (int) Math.ceil (ImgHeight/(float) targetheight); Opts.insamplesize = 1; if (WidthRatio > 1 | | widthRatio > 1) {if (WidthRatio > HeightRatio) {opts.insamp Lesize = WidthRatio; } else {opts.insamplesize = HeightRatio; }}//Set the zoom scale, load the picture into the content; opts.injustdecodebounds = false; Bitmap = BitmapfactOry.decodefile (PathName, opts); return bitmap; }
second, the compressed image is stored in the SD card:
stored in SD card public void SaveFile (Bitmap BM, String filename) throws Exception { file Dirfile = new File (fileName);
//detects if the picture exists if (dirfile.exists ()) { dirfile.delete (); Delete Original image } file Mycapturefile = new file (fileName); Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (Mycapturefile)); 100 means no compression, 70 indicates a compression rate of 30% bm.compress (Bitmap.CompressFormat.JPEG, N, Bos); Bos.flush (); Bos.close (); }
note here that, due to the need to write the SD card, add a permission:
<!--write SD card-- <uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
third, attach a complete small demo:
1) Mainactivity.java
Package Com.face.sendwinrar;import Java.io.bufferedoutputstream;import Java.io.file;import Java.io.fileoutputstream;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.os.bundle;import Android.view.menu;import Android.view.MenuItem; public class Mainactivity extends Activity {//Photo save address private static final String File_path = "/sdcard/gone.jpg"; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); try {//compress picture Bitmap Bitmap = Compressbysize (file_path,150,200); Save Picture SaveFile (Bitmap, File_path); } catch (Exception e) {e.printstacktrace ();} }//Compressed picture size public Bitmap compressbysize (String pathName, int targetwidth, int targetheight) { Bitmapfactory.options opts = new Bitmapfactory.options (); Opts.injustdecodebounds = true;//not to really parse the picture, just get the diagramThe head information of the piece contains the width higher; Bitmap Bitmap = Bitmapfactory.decodefile (PathName, opts); Get the width and height of the picture; float imgwidth = opts.outwidth; float imgheight = opts.outheight; Calculates the ratio of the width of the picture, the height to the target width, the height, and the smallest integer greater than or equal to the ratio; int widthRatio = (int) Math.ceil (ImgWidth/(float) targetwidth); int heightratio = (int) Math.ceil (ImgHeight/(float) targetheight); Opts.insamplesize = 1; if (WidthRatio > 1 | | widthRatio > 1) {if (WidthRatio > HeightRatio) {opts.insamp Lesize = WidthRatio; } else {opts.insamplesize = HeightRatio; }}//Set the zoom scale, load the picture into the content; opts.injustdecodebounds = false; Bitmap = Bitmapfactory.decodefile (PathName, opts); return bitmap; }//stored into SD card public void SaveFile (Bitmap BM, String fileName) throws Exception {file Dirfile = new File (Filena ME); Detects if a picture exists if (dirfile.exists()) {dirfile.delete (); Delete Original image} file Mycapturefile = new file (fileName); Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (Mycapturefile)); 100 means no compression, 70 indicates a compression rate of 30% bm.compress (Bitmap.CompressFormat.JPEG, N, BOS); Bos.flush (); Bos.close (); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Act Ion Bar if it is present. Getmenuinflater (). Inflate (R.menu.main, menu); return true; } @Override public boolean onoptionsitemselected (MenuItem Item) {//Handle Action Bar item clicks here. The action bar would//automatically handle clicks on the Home/up button, so long/As you specify a parent Activity in Androidmanifest.xml. int id = item.getitemid (); if (id = = r.id.action_settings) {return true; } return super.onoptionsitemselected (item); }}
2) Mainfest
<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "package=" Com.dc.xust.paybyface "android:versioncode=" 1 "android:versionname=" 1.0 "> <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>" <!--write SD card--<uses-permission a Ndroid:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowbackup= "true" a ndroid:icon= "@drawable/ic_launcher" android:label= "@string/app_name" android:theme= "@style/apptheme" > <activity android:name= ". Mainactivity "android:label=" @string/app_name "> <intent-filter> <action Android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUNCHER "/> </intent-filter> </activity> </application></manifest>
here directly run on OK, do not need interface, Main_activity.xml file directly is the default, here is not attached to.
Reference: A few days ago the webpage, now can not find, found on the Tim.
Original address: http://blog.csdn.net/qingdujun/article/details/39480117
Android camera to take photos, compress and store in SD card