Android call system take pictures of those things

Source: Internet
Author: User

Childhood know every day to write a diary is a good habit, slowly found that they forget these habits, long time did not give this dry blog add a little new, this back also appeared a small bud to refresh a blog.

Today's writing is not complicated, is to review some of the old knowledge, but also to remember a note, good gossip not much to say, start today's theme it.

About taking pictures, this is not the implementation of their own photos, is the call system to take pictures, very simple, but sometimes I also encountered a problem, is that I did not actively compress, the system is automatically help me to compress, but I need these high-definition pictures, the solution online also have said, but I do is my own notes, so I do not care At the very least, I was verified before I wrote my notes.

Here is how the system default compressed pictures are called

1     Private Static Final int Take_picture = 0x000001; 2      Public void photo () {3         New Intent (mediastore.action_image_capture); 4         Startactivityforresult (opencameraintent, take_picture); 5     }
@Overrideprotected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) {    Switch(requestcode) { Casetake_picture:if(&& ResultCode = =RESULT_OK) {String FileName=string.valueof (System.currenttimemillis ()); Bitmap BM=NULL; Contentresolver Resolver=Getcontentresolver (); if(Originaluri = =NULL) {BM= (Bitmap) Intent.getextras (). Get ("Data"));// Xiaomi 5 test found like walking here, Huawei G521 android4.3 old machine also go here }Else {                    Try{BM=Bitmapfactory.decodestream (Resolver.openinputstream (Originaluri));LOGUTIL.E (TAG, "Originalbitmap photo width=" +bm.getwidth ()); BM=Comp (BM);//Here is the compression as for the compression method, put down the surface provided}Catch(FileNotFoundException e) {e.printstacktrace (); }                }          
Left to operate the bitmap, such as the following save
Fileutils.savebitmap (BM, fileName);
//........
}
Break ;
This. Mloginhelper.onactivityresult (Requestcode, ResultCode, intent);
}

The above is the usual method of invocation, the disadvantage is that the image will be compressed by the system itself.

Not in circles, the following method is the way to save the original image.

1     Private Static Final intTake_picture = 0x000001;2     PrivateString FileName;3 4      Public voidphoto () {5Intent opencameraintent =NewIntent (mediastore.action_image_capture);6FileName =string.valueof (System.currenttimemillis ());7File Photofile =fileutils.createpic (fileName);8 Opencameraintent.putextra (Mediastore.extra_output, Uri.fromfile (Photofile));9 Startactivityforresult (opencameraintent, take_picture);Ten}

Returns the call

1 @Override2     protected voidOnactivityresult (intRequestcode,intResultCode, Intent Intent) { $         Switch(requestcode) {Panax Notoginseng          Casetake_picture: -             if(Bimp.tempSelectBitmap.size () < 3 && ResultCode = =RESULT_OK) { -Uri Originaluri =NULL; $File f=fileutils.getpic (fileName); $                 Try { -Originaluri = Uri.parse (MediaStore.Images.Media.insertImage (Getcontentresolver (), F.getabsolutepath (),NULL,NULL)); -}Catch(FileNotFoundException e) { the e.printstacktrace (); -                 } theBitmap BM =NULL; -Contentresolver resolver =getcontentresolver (); About                 if(Originaluri = =NULL) { -BM = (Bitmap) Intent.getextras (). Get ("Data")); -}Else { -                     Try { ABM =Bitmapfactory.decodestream (Resolver.openinputstream (Originaluri)); theLOGUTIL.E (TAG, "Originalbitmap photo width=" +bm.getwidth ()); $BM =Comp (BM); the}Catch(FileNotFoundException e) { the e.printstacktrace (); the                     } the  -                 } in Fileutils.savebitmap (BM, fileName); the  theContinue operation +  -             } the              Break;Bayi         } the          This. Mloginhelper.onactivityresult (Requestcode, ResultCode, intent); the}

Here's how to save it:

 Public Static voidSavebitmap (Bitmap BM, String picname) {Try {            if(!isfileexist ("") ) {File Tempf= Createsddir (""); } File F=NewFile (Sdpath, Picname + ". JPEG "); if(F.exists ()) {f.delete (); } FileOutputStream out=NewFileOutputStream (f); Bm.compress (Bitmap.CompressFormat.JPEG,100, out);            Out.flush ();        Out.close (); } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }    }

Here is the compression method:

PrivateBitmap Comp (Bitmap image) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,100, BAOs); if(Baos.tobytearray (). length/1024 > 1024) {//determine if the picture is larger than 1M, compress to avoid overflow when generating picture (Bitmapfactory.decodestream)Baos.reset ();//Reset BAOs is empty BAOsImage.compress (Bitmap.CompressFormat.JPEG, BAOs);//this compresses 50% and stores the compressed data in the BAOs .} bytearrayinputstream ISBM=NewBytearrayinputstream (Baos.tobytearray ()); Bitmapfactory.options newopts=Newbitmapfactory.options (); //start reading the picture, this time set the Options.injustdecodebounds back to TrueNewopts.injustdecodebounds =true; Bitmap Bitmap= Bitmapfactory.decodestream (ISBM,NULL, newopts); Newopts.injustdecodebounds=false; intW =Newopts.outwidth; inth =Newopts.outheight; //now the mainstream phone is more 800*480 resolution, so the height and width we set to        floatHH = 1000f;//set height to 800f here        floatWW = 1000f;//This sets the width to 480f//scaling ratio. Because it is a fixed scale, only one of the data with high or wide is calculated        intbe = 1;//be=1 indicates no scaling        if(W > H && w > WW) {//If the width is large, scale according to the fixed width sizebe = (int) (Newopts.outwidth/ww); } Else if(W < h && h > hh) {//if height is high, it is scaled according to width fixed sizebe = (int) (Newopts.outheight/hh); }        if(Be <= 0) be= 1; Newopts.insamplesize= be;//set the zoom ratio//re-read the picture and note that the Options.injustdecodebounds has been set back to false at this timeISBM =NewBytearrayinputstream (Baos.tobytearray ()); Bitmap= Bitmapfactory.decodestream (ISBM,NULL, newopts); returnCompressimage (bitmap);//compress the proportions and then compress the mass.    }    PrivateBitmap compressimage (Bitmap image) {Bytearrayoutputstream BAOs=NewBytearrayoutputstream (); Image.compress (Bitmap.CompressFormat.JPEG,, BAOs);//mass compression method, where 100 means no compression, the compressed data stored in the BAOs        intoptions = 100;//While (Baos.tobytearray (). length/1024 >) {//loop to determine if the image is larger than 100kb after compression         while(Baos.tobytearray (). length/1024 > 2048) {//loop to determine if the image is larger than 1024kb after compressionBaos.reset ();//Reset BAOs is empty BAOsImage.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//this compresses the options% and stores the compressed data in the BAOs .Options-= 10;//each time, it's reduced by ten} bytearrayinputstream ISBM=NewBytearrayinputstream (Baos.tobytearray ());//Store the compressed data BAOs in the BytearrayinputstreamBitmap Bitmap = Bitmapfactory.decodestream (ISBM,NULL,NULL);//generate images from the Bytearrayinputstream data        returnbitmap; }

Android call system take pictures of those things

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.