`

Android 图片加水印

阅读更多

Android 图片加水印

首先可以肯定 java.awt 在android中是不可用的

直接上代码:

/**
     * 
     * @param src 原图片
     * @param watermark  要打的水印图片
     * @param title  要打的水印文字
     * @param densityDpi  屏幕位深密度
     * @return Bitmap 打好水印的图片
     */
	public static Bitmap createBitmap(String file, Bitmap watermark, String title,int densityDpi) {
		int dpi = densityDpi;
		LogPrint.Print("watermark===createBitmap====dpi"+dpi);
		// 读取原图片信息  
        File srcImgFile = new File(file); 
		Bitmap roratesrc = BitmapFactory.decodeFile(file);
		Bitmap  src = rorateBitamp(90, roratesrc);
		if (src == null) {
			
			return null;
		}
		int srcWidth = src.getWidth();
		int srcHeight = src.getHeight();

		Paint paint = new Paint();
		// ARGB_8888
		Bitmap newb = Bitmap
				.createBitmap(srcWidth, srcHeight, Config.ARGB_8888);// 创建一个新的和src长度宽度一样的位图
		// 把创建的位图作为画板
		Canvas cv = new Canvas(newb);
		// 在0,0坐标开始画入src
		cv.drawBitmap(src, 0, 0, paint);
		if (watermark != null) {
			int ww = watermark.getWidth();
			int wh = watermark.getHeight();
			paint.setAlpha(50);
			// cv.drawBitmap(watermark, srcWidth - ww + 1, srcHeight - wh +1,paint);// 在src的右下角画入水印
			cv.drawBitmap(watermark, 1, 1, paint);// 在src的左上角画入水印
		}
		// 加入文字
		if (title != null) {
			String familyName = "宋体";
			Typeface font = Typeface.create(familyName, Typeface.NORMAL);
			TextPaint textPaint = new TextPaint();
			textPaint.setColor(Color.WHITE);
			textPaint.setTypeface(font);
			if(dpi <= 120){//qvga 240X400
				textPaint.setTextSize(5);
			}else if(dpi <= 160){//hvga 320X480
				textPaint.setTextSize(8);
			}else if(dpi <= 240){//wvga 480X800
				textPaint.setTextSize(10);
				if (Configs.WIDTH > 700) {
					
					textPaint.setTextSize(15);
				}
			}else if(dpi <= 320){// 1280*720
				
				textPaint.setTextSize(15);
				if (Configs.WIDTH > 2000 ) {
					
					textPaint.setTextSize(72);
				}
				
			}else { //更大屏幕分辨率
				
				textPaint.setTextSize(72);
			}
			// 这里是自动换行的
			StaticLayout layout = new StaticLayout(title, textPaint, srcWidth,
					Alignment.ALIGN_NORMAL, 1.0F, 0.0F, true);
			layout.draw(cv);
			// 文字加在左上角
			//cv.drawText(title,40,40,paint);
		} else {

			paint.setColor(Color.WHITE);
			paint.setTextSize(20);
			cv.drawText("测试", 1, 1, paint);
		}
		cv.save(Canvas.ALL_SAVE_FLAG);// 保存
		cv.restore();// 存储
	
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		
		newb.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] bytes = baos.toByteArray();
		try {
			FileOutputStream fileOutputStream = new FileOutputStream(srcImgFile);
			fileOutputStream.write(bytes);
			fileOutputStream.flush();
			fileOutputStream.close();
		} catch (IOException e) {

			e.printStackTrace();
		}
		return newb;
	}

 示例:

//图片添加文字水
if (mlocation != null) {
					
           BitmapUtils.createBitmap(mPictureFile.getAbsolutePath(), null,"经纬度:"+mlocation.getLongitude()+","+mlocation.getLatitude()+"\t\t角度:"+mlocation.getBearing()+"度\t\t"+"方向:"+sensorOrientation+"度\t\t速度:"+mlocation.getSpeed()+"公里/小时\n拍摄时间:"+imageFormat.format(System.currentTimeMillis()),getResources().getDisplayMetrics().densityDpi);
	            	
   }else {
					
	    BitmapUtils.createBitmap(mPictureFile.getAbsolutePath(), null,"经纬度:"+0+","+0+"\t\t角度:"+0+"度\t\t"+"方向:"+sensorOrientation+"度\t\t速度:"+0+"公里/小时\n拍摄时间:"+imageFormat.format(System.currentTimeMillis()),getResources().getDisplayMetrics().densityDpi);
   }

  

 

 

  • 大小: 142.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics