Android Image Processing (image synthesis, image rounded corner, image flip, image scaling)

Source: Internet
Author: User
Tags image flip

Image Synthesis

/**
* Image Synthesis
* @ Param bitmap
* @ Return
*/
Private Bitmap createBitmap (Bitmap src, Bitmap watermark ){
If (src = null ){
Return null;
}
Int w = src. getWidth ();
Int h = src. getHeight ();
Int ww = watermark. getWidth ();
Int wh = watermark. getHeight ();
// Create the new blank bitmap
Bitmap newb = Bitmap. createBitmap (w, h, Config. ARGB_8888); // create a new Bitmap with the same length and width as SRC.
Canvas cv = new Canvas (newb );
// Draw src
Cv. drawBitmap (src, 0, 0, null); // draw src at the coordinates of 0, 0.
// Draw watermark
Cv. drawBitmap (watermark, w-ww + 5, h-wh + 5, null); // Add a watermark to the bottom right corner of src.
// Save all clip
Cv. save (Canvas. ALL_SAVE_FLAG); // save
// Store
Cv. restore (); // Storage
Return newb;
}

Rounded corner

/**
* Rounded corner
* @ Param bitmap
* @ Return
*/
Public static Bitmap getRoundedCornerBitmap (Bitmap bitmap ){
Bitmap output = Bitmap. createBitmap (bitmap. getWidth (),
Bitmap. 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 );
Final float roundPx = 12;

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;
}

Resize, flip, and rotate Images

/**
* Scale, flip, and rotate Images
* @ Param BMP
* @ Param rotate
* @ Return
*/
Private android. graphics. Bitmap gerZoomRotateBitmap (
Android. graphics. Bitmap BMP org, int rotate ){
// Obtain the original image size
Int width = BMP org. getWidth ();
Int height = BMP org. getHeight ();

Int newWidth = 300;
Int newheight = 300;
// Define the ratio of the zoom height to the width.
Float sw = (float) newWidth)/width;
Float sh = (float) newheight)/height;
// Create a Matrix object used to operate Images
Android. graphics. Matrix matrix = new android. graphics. Matrix ();
// Scale and flip the image
// The absolute value of sw sh is the ratio of height to bloom width. If sw is negative, it indicates X-direction flip. If sh is negative, it indicates Y-direction flip.
Matrix. postScale (sw, sh );
// Rotate 30 *
Matrix. postRotate (rotate );
// Create a new image
Android. graphics. Bitmap resizeBitmap = android. graphics. Bitmap
. CreateBitmap (BMP org, 0, 0, width, height, matrix, true );
Return resizeBitmap;
}

Related Article

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.