Large bitmap compression watermark processing for Android development

Source: Internet
Author: User

We can add a watermark to the image when we tweet or Csdn blog posts. An independent identity, how is that implemented? Encapsulates a Bitmaptools wrapper class first. The problem that this class solves is that a breakthrough is stored in the SDcard, and the image is added to the watermark.

Bitmaptools

Package Com.example.g11_bitmap04;import Java.io.file;import Java.io.fileoutputstream;import java.io.OutputStream; Import Android.content.res.resources;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.paint;import Android.os.environment;public class BitmapTools { Public Bitmaptools () {//TODO auto-generated constructor stub}public static Bitmap CreateBitmap (Resources resources, int R Esid,string name) {Bitmap Bitmap=bitmapfactory.decoderesource (resources, RESID);//Copy a new Bitmap, Since the watermark operation cannot be performed directly on the original Bitmap//bitmap.config the stored format Bitmap newbitmap=bitmap.copy (Bitmap.Config.ARGB_8888, true);// Canvas canvas=new Canvas (newbitmap) using your own definition canvases; Paint paint=new Paint ();p aint.settextsize (), Canvas.drawtext ("Hello", [+], "paint"),//infer whether SDcard is in the available state if ( Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {//Save the picture directly under the root folder file root= Environment.getexternalstoragedirectory (); OutputStream Outputstream=null;try {outputstream=New FileOutputStream (root, name);//compress the image in PNG format and save it in SDcard newbitmap.compress ( Bitmap.CompressFormat.PNG, OutputStream);} catch (Exception e) {//Todo:handle Exception}}return Newbitmap;}}

This method is called in mainactivtiy

Package Com.example.g11_bitmap04;import Android.os.bundle;import Android.app.activity;import Android.graphics.bitmap;import Android.view.menu;import Android.view.view;import Android.widget.Button;import     Android.widget.imageview;public class Mainactivity extends Activity {private ImageView ImageView; Private button button, @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate ); Setcontentview (R.layout.activity_main); imageview= (ImageView) This.findviewbyid (r.id.imageview1); Button= ( Button) This.findviewbyid (R.id.button1), Button.setonclicklistener (new View.onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated method Stubbitmap Bitmap=bitmaptools.createbitmap (Getresources (), R.drawable.a , "A.png"); Imageview.setimagebitmap (bitmap);}}); @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}



Large bitmap compression watermark processing for Android development

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.