Android Image Processing-compression, cropping, rounded corners, and storage,
Processing of images used in the project
Public class UtilPicture {public static final String IMAGE_UNSPECIFIED = "image/*";/*** store the image to the SD card. Determine whether the SD card is installed, whether it can be read/written, and whether it has space, otherwise, an error is prompted * @ param ctx context * @ param jpeg. The photo to be stored * @ param quality: the best quality of the compressed photo. The value ranges from 0 to 100,100, generally, the path stored in the 80-90 * @ param filePath * @ param filename photo name * @ return */public static boolean save_picture (Context ctx, Bitmap bitmap, int quality, String filePath, string filename) {ByteArrayOutpu TStream baos = new ByteArrayOutputStream (); bitmap. compress (Bitmap. CompressFormat. JPEG, quality, baos); byte [] data = baos. toByteArray (); if (! Common. checkSDStatus (data. length/1024/1024) {Toast. makeText (ctx, "your storage card has an error", Toast. LENGTH_SHORT ). show (); return false;} try {File destDir = new File (filePath); if (! DestDir. exists () destDir. mkdirs (); String path = filePath + "/" + filename; File file = new File (path); if (! File. exists () file. createNewFile (); FileOutputStream fos = new FileOutputStream (file); fos. write (data); fos. close ();} catch (Exception e) {e. printStackTrace (); return false;} return true ;} /*** method for obtaining the rounded corner image * @ param bitmap the image to be processed * @ param roundPx the arc rate of the rounded corner * @ return */public static Bitmap getRoundedCornerBitmap (Bitmap bitmap, float roundPx) {Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), B Itmap. getHeight (), Config. ARGB_8888); Canvas canvas = new Canvas (output); final int color = 0xff0000242; final Paint paint = new Paint (); final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight (); final RectF rectF = new RectF (rect); paint. setAntiAlias (true); canvas. drawARGB (0, 0, 0, 0); paint. setColor (color); canvas. drawRoundRect (rectF, roundPx, roundPx, paint); paint. setXfermode (new Porterduxfermode (Mode. SRC_IN); canvas. drawBitmap (bitmap, rect, rect, paint); return output ;} /*** input the GPS and time text in the image * @ param bitmap the image to be processed * @ param datetime time * @ param lat longitude * @ param lng latitude * @ return */ public static Bitmap getGpsBitmap (Bitmap bitmap, string datetime, String lat, String lng) {Bitmap output = Bitmap. createBitmap (bitmap. getWidth (), bitmap. getHeight (), Config. ARGB_8888);/* bitmap Write it into the canvas class */Canvas canvas = new Canvas (output);/* canvas area */final Rect rect = new Rect (0, 0, bitmap. getWidth (), bitmap. getHeight ();/* spray Paint class */Paint paint = new Paint (); paint. setAntiAlias (true); // removes the Sawtooth paint. setColor (Color. RED); // color paint. setTextSize (16); // font size canvas. drawText ("longitude:" + lng, 10, 20, paint); canvas. drawText ("latitude:" + lat, 10, 38, paint); canvas. drawText ("time:" + datetime, 10, 56, Paint); paint. setXfermode (new porterduduxfermode (Mode. DST_ATOP); canvas. drawBitmap (bitmap, rect, rect, paint); return output;}/***** crop an image * @ param originFile source file * @ param TargetFile target file * @ param aspect width/height ratio, if it is null, * @ param output resolution * @ return */public static Intent startPhotoZoom (File originFile, File TargetFile, int [] aspect, int [] output) is not limited) {Intent intent = new Intent ("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 = new BitmapFactory. options (); op. inJustDecodeBounds = true; BitmapFactory. decodeFile (originFile. getPath (), op); int jpgWidth = op. outWidth; int jpgHeight = 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 (); return intent;}/*** after selecting a photo from the album, obtain the absolute path * @ param ctx * @ param photoUri * @ return */public static String getPickPhotoPath (Context ctx, Uri photoUri ){ Cursor cursor = null; try {cursor = ctx. getContentResolver (). query (photoUri, null, null); cursor. moveToFirst (); String imgPath = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); return imgPath;} catch (Exception e) {return "";} finally {cursor. close () ;}} public static File getPickPhotoFile (Context ctx, Uri photoUri) {String imgPath = getPickPhotoPa Th (ctx, photoUri); if (! TextUtils. isEmpty (imgPath) return new File (imgPath); else return null;}/*** compresses the image size to avoid excessive image size and keep the proportion unchanged, the width or height cannot exceed XX pixels * @ param newName new file name * @ param filePath original file full path, including file name * @ param attachPath after processing, file storage location * @ param String new file full path */public static String compressPixelPhotos (final Context ctx, final String newName, final String filePath, final String attachPath) {BitmapFactory. options op = new BitmapFact Ory. options (); op. inJustDecodeBounds = true; BitmapFactory. decodeFile (filePath, op); int jpgWidth = op. outWidth; int jpgHeight = op. outHeight; if (jpgWidth> 800 || jpgHeight> 800) {int wSendRatio = (int) Math. ceil (jpgWidth/800366f); int hSendRatio = (int) Math. ceil (jpgHeight/800366f); 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, 90, attachPath, newName) {Common. copyFileToFile (filePath, attachPath + File. separator + newName);} if (B! = Null &&! B. isRecycled () B. recycle ();} else {Common. copyFileToFile (filePath, attachPath + File. separator + newName);} return attachPath + File. separator + newName;}/*** check the image resolution and whether compression is required * @ param ctx * @ param filePath * @ return */public static boolean compressPixelPhotosCheck (final Context ctx, final String filePath) {BitmapFactory. options op = new BitmapFactory. options (); op. inJustDecodeBounds = tru E; BitmapFactory. decodeFile (filePath, op); if (op. outWidth> 800 | op. outHeight> 800) {return true;} else {return false;}/*** put the * @ param filename file name, full path * @ param jpgGetWidth photo width * @ param jpgGetHeight photo height * @ return */public static Bitmap decodeFile (String filename, int jpgGetWidth, int jpgGetHeight) {Bitmap B = null; try {BitmapFactory. options op = new BitmapFactory. options (); op. inJu StDecodeBounds = true; BitmapFactory. decodeFile (filename, op); int jpgWidth = op. outWidth; int jpgHeight = op. outHeight; int wSendRatio = (int) Math. ceil (jpgWidth/Double. valueOf (jpgGetWidth); int hSendRatio = (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) {} return B ;}}
More discussion groups can be added:71262831
Let's take a look at the changing situation. Scan the QR code below to follow the it talents (You can also search:It talents).
Push the latest development resources for you and share your it career development experience: