Android image processing-compress, trim, fillet, save

Source: Internet
Author: User

The processing of pictures used in the project

 Public  class utilpicture {     Public Static FinalString image_unspecified ="image/*";/** * Save the image to an SD card, you need to determine whether the SD card is installed, whether it can read and write, whether there is room, otherwise error * @param CTX Context * @param JPEG photos to be stored * @param Quality compressed photo quality, 0 to 100,100 Best, General 80-90 * @param filePath stored path * @param filename The name of the photo * @return * *     Public Static Boolean save_picture(Context ctx, Bitmap Bitmap,intQuality, string filePath, string filename) {Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.JPEG, quality, BAOs);byte[] data = Baos.tobytearray ();if(! Common.checksdstatus (data.length/1024x768/1024x768) {Toast.maketext (CTX,"Your memory card has errors", Toast.length_short). Show ();return false; }Try{File DestDir =NewFile (FilePath);if(!destdir.exists ()) destdir.mkdirs (); String Path = FilePath +"/"+ filename; File File =NewFile (path);if(!file.exists ()) file.createnewfile (); FileOutputStream fos =NewFileOutputStream (file);            Fos.write (data);        Fos.close (); }Catch(Exception e) {E.printstacktrace ();return false; }return true; }/** * How to get rounded pictures * @param bitmap image to be processed * @param The arc rate of the ROUNDPX fillet * @return */     Public StaticBitmapGetroundedcornerbitmap(Bitmap Bitmap,floatROUNDPX) {Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888); Canvas Canvas =NewCanvas (output);Final intcolor =0xff424242;FinalPaint paint =NewPaint ();FinalRect rect =NewRect (0,0, Bitmap.getwidth (), Bitmap.getheight ());FinalRECTF RECTF =NewRECTF (rect); Paint.setantialias (true); Canvas.drawargb (0,0,0,0);        Paint.setcolor (color);        Canvas.drawroundrect (RECTF, ROUNDPX, ROUNDPX, paint); Paint.setxfermode (NewPorterduffxfermode (mode.src_in)); Canvas.drawbitmap (Bitmap, rect, rect, paint);returnOutput }/** * Pictures in the picture of GPs and time and other text * @param bitmap need to process pictures * @param datetime time * @param L At Longitude * @param LNG latitude * @return  * *     Public StaticBitmapGetgpsbitmap(Bitmap Bitmap, String datetime, String lat, string lng) {Bitmap output = Bitmap.createbitmap (Bitmap.getwidth (), Bitmap.getheight (), config.argb_8888);/ * Write the bitmap in canvas class * /Canvas Canvas =NewCanvas (output);/ * Area of the canvas * /        FinalRect rect =NewRect (0,0, Bitmap.getwidth (), Bitmap.getheight ());/ * Paint Paint class * /Paint paint =NewPaint (); Paint.setantialias (true);//anti-aliasingPaint.setcolor (color.red);//ColoringPaint.settextsize ( -);//font sizeCanvas.drawtext ("Longitude:"+ LNG,Ten, -, paint); Canvas.drawtext ("Latitude:"+ lat,Ten, -, paint); Canvas.drawtext ("Time:"+ datetime,Ten, About, paint); Paint.setxfermode (NewPorterduffxfermode (Mode.dst_atop)); Canvas.drawbitmap (Bitmap, rect, rect, paint);returnOutput }/** * Cut pictures * @param originfile source file * @param targetfile target file * @param aspect The width-height ratio, if null, does not limit the * @param Output Resolution * @return  */     Public StaticIntentStartphotozoom(File originfile, file TargetFile,int[] aspect,int[] output) {Intent Intent =NewIntent ("Com.android.camera.action.CROP");        Intent.setdataandtype (Uri.fromfile (originfile), image_unspecified); Intent.putextra ("Crop","true"); Intent.putextra ("Nofacedetection",true); Intent.putextra ("Return-data",false);if(NULL! = Output) {bitmapfactory.options op =NewBitmapfactory.options (); Op.injustdecodebounds =true; Bitmapfactory.decodefile (Originfile.getpath (), op);intJpgwidth = Op.outwidth;intJpgheight = Op.outheight;if(Jpgwidth > output[0] && jpgheight > output[1]) {Intent.putextra ("Outputx", output[0]); Intent.putextra ("Outputy", output[1]); }        }if(NULL! = aspect) {Intent.putextra ("Aspectx", aspect[0]); Intent.putextra ("Aspecty", aspect[1]); }if(! Targetfile.exists ())Try{Targetfile.createnewfile (); }Catch(IOException e)            {E.printstacktrace ();        } Intent.putextra (Mediastore.extra_output, Uri.fromfile (targetfile)); Intent.putextra ("OutputFormat", Bitmap.CompressFormat.JPEG.toString ());returnIntent }/** * After selecting a photo from the album, get the absolute path to the photo * @param ctx * @param Photouri * @return */     Public StaticStringGetpickphotopath(Context ctx, Uri Photouri) {CURSOR cursor =NULL;Try{cursor = Ctx.getcontentresolver (). Query (Photouri,NULL,NULL,NULL,NULL);            Cursor.movetofirst (); String Imgpath = cursor.getstring (Cursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA));returnImgpath; }Catch(Exception e) {return ""; }finally{Cursor.close (); }    } Public StaticFileGetpickphotofile(Context ctx, Uri Photouri) {String Imgpath = Getpickphotopath (CTX, Photouri);if(! Textutils.isempty (Imgpath))return NewFile (Imgpath);Else            return NULL; }/** * Compress the picture size, avoid the picture too large, keep the proportions unchanged, width or height not more than xx pixels * @param newName New file name * @param filePath The original file full path, Contains file name * @param attachpath after processing, the location of files * @param String new File full path */     Public StaticStringCompresspixelphotos(FinalContext CTX,FinalString NewName,FinalString FilePath,FinalString attachpath) {bitmapfactory.options op =NewBitmapfactory.options (); Op.injustdecodebounds =true; Bitmapfactory.decodefile (FilePath, op);intJpgwidth = Op.outwidth;intJpgheight = Op.outheight;if(Jpgwidth > -|| Jpgheight > -) {intWsendratio = (int) Math.ceil (Jpgwidth/800.0f);intHsendratio = (int) Math.ceil (Jpgheight/800.0f);if(Wsendratio >1&& hsendratio >1) {op.insamplesize = wsendratio > hsendratio? wsendratio:hsendratio; } op.injustdecodebounds =false; Bitmap B = Bitmapfactory.decodefile (FilePath, op);if(!save_picture (CTX, B, -, Attachpath, NewName)) {common.copyfiletofile (FilePath, Attachpath + file.separator + newName); }if(b! =NULL&&!b.isrecycled ()) b.recycle (); }Else{Common.copyfiletofile (FilePath, Attachpath + file.separator + newName); }returnAttachpath + file.separator + newName; }/** * Check the image resolution size, need to compress * @param ctx * @param filePath * @return * /     Public Static Boolean Compresspixelphotoscheck(FinalContext CTX,FinalString filePath) {bitmapfactory.options op =NewBitmapfactory.options (); Op.injustdecodebounds =true; Bitmapfactory.decodefile (FilePath, op);if(Op.outwidth > -|| Op.outheight > -) {return true; }Else{return false; }    }/** * @param filename, full path * @param jpggetwidth Photo width * @param Jpggeth Eight Photo high * @return * /     Public StaticBitmapDecodeFile(String filename,intJpggetwidth,intJpggetheight) {Bitmap b =NULL;Try{Bitmapfactory.options op =NewBitmapfactory.options (); Op.injustdecodebounds =true; Bitmapfactory.decodefile (filename, op);intJpgwidth = Op.outwidth;intJpgheight = Op.outheight;intWsendratio = (int) Math.ceil (jpgwidth/double.valueof (jpggetwidth));intHsendratio = (int) Math.ceil (jpgheight/double.valueof (jpggetheight));if(Wsendratio >1&& hsendratio >1) {op.insamplesize = wsendratio > hsendratio? wsendratio:hsendratio; } op.injustdecodebounds =false;        b = bitmapfactory.decodefile (filename, op); }Catch(Exception e) {        }returnb }}

More communication and technical discussion group:71262831
Sweep, sit and look at the changeable. Scan the QR code below to focus on it talent (also searchable:it talent ).
For you to push the latest development resources, share it cattle career development experience:

Android image processing-compress, trim, fillet, save

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.