zoukankan      html  css  js  c++  java
  • android 文字图片合成

    引用:http://blog.csdn.net/cq361106306/article/details/8142526

    1. 两种方法:  
    2.   
    3. 1.直接在图片上写文字  
    4.   
    5.    
    6.   
    7.         String str = "PICC要写的文字";  
    8.   
    9.        ImageView image = (ImageView) this.findViewById(R.id.ImageView);  
    10.        Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);  
    11.        int width = photo.getWidth(), hight = photo.getHeight();  
    12.        System.out.println("宽"+width+"高"+hight);  
    13.        icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap    
    14.        Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到icon上    
    15.           
    16.        Paint photoPaint = new Paint(); //建立画笔    
    17.        photoPaint.setDither(true); //获取跟清晰的图像采样    
    18.        photoPaint.setFilterBitmap(true);//过滤一些    
    19.           
    20.        Rect src = new Rect(00, photo.getWidth(), photo.getHeight());//创建一个指定的新矩形的坐标    
    21.        Rect dst = new Rect(00, width, hight);//创建一个指定的新矩形的坐标    
    22.        canvas.drawBitmap(photo, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint    
    23.           
    24.        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔    
    25.        textPaint.setTextSize(20.0f);//字体大小    
    26.        textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度    
    27.        textPaint.setColor(Color.RED);//采用的颜色    
    28.        //textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的设置    
    29.        canvas.drawText(str, 2026, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制   
    30.        canvas.save(Canvas.ALL_SAVE_FLAG);   
    31.        canvas.restore();   
    32.        image.setImageBitmap(icon);  
    33.        saveMyBitmap(icon);  
    34.   
    35.    
    36.   
    37. 2.将两个图片合成  
    38.   
    39.       onCreat方法里面{  
    40.   
    41.          Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);   
    42.             Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);  
    43.   
    44.         Bitmap a = createBitmap(photo,mark);  
    45.             image.setImageBitmap(a);  
    46.             saveMyBitmap(a);  
    47.   
    48.        }  
    49.   
    50.    
    51.   
    52.         
    53.   
    54.     private Bitmap createBitmap( Bitmap src, Bitmap watermark )  
    55.   
    56.     {  
    57.   
    58.     String tag = "createBitmap";  
    59.   
    60.    // Log.d( tag, "create a new bitmap" );  
    61.   
    62.     if( src == null )  
    63.   
    64.     {  
    65.   
    66.     return null;  
    67.   
    68.     }  
    69.   
    70.     int w = src.getWidth();  
    71.   
    72.     int h = src.getHeight();  
    73.   
    74.     int ww = watermark.getWidth();  
    75.   
    76.     int wh = watermark.getHeight();  
    77.   
    78.     //create the new blank bitmap  
    79.   
    80.     Bitmap newb = Bitmap.createBitmap( w, h, Config.ARGB_8888 );  
    81.     //创建一个新的和SRC长度宽度一样的位图  
    82.   
    83.     Canvas cv = new Canvas( newb );  
    84.   
    85.     //draw src into  
    86.   
    87.     cv.drawBitmap( src, 00null );//在 0,0坐标开始画入src  
    88.   
    89.     //draw watermark into  
    90.   
    91.     cv.drawBitmap( watermark, w - ww + 5, h - wh + 5null );//在src的右下角画入水印  
    92.   
    93.     //save all clip  
    94.   
    95.     cv.save( Canvas.ALL_SAVE_FLAG );//保存  
    96.   
    97.     //store  
    98.   
    99.     cv.restore();//存储  
    100.   
    101.     return newb;  
    102.   
    103.     }  
    104.   
    105.    
    106.   
    107. //保存图片到data下面  
    108.   
    109.       
    110.   
    111.     public void saveMyBitmap(Bitmap bmp){  
    112.      FileOutputStream fos = null;  
    113.         try {  
    114.             fos = openFileOutput("image1.jpg", Context.MODE_PRIVATE);  
    115.             bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);  
    116.         } catch (FileNotFoundException e) {  
    117.         } finally {  
    118.             if (fos != null) {  
    119.                 try {  
    120.                     fos.flush();  
    121.                     fos.close();  
    122.                 } catch (IOException e) {  
    123.                 }  
    124.             }  
    125.         }  
    126.   
    127.     }  
  • 相关阅读:
    ANDROID BINDER机制浅析
    ANDROID权限机制
    运算符
    Give root password for maintenance
    安装python工具
    gitlab
    jumpserver
    python环境安装
    inode
    升级openssh漏洞
  • 原文地址:https://www.cnblogs.com/sode/p/3148502.html
Copyright © 2011-2022 走看看