-----------------------------
1. 圖像處理
-----------------------------
1. 圖片加字 :在 Canvas上,使用 drawText 方法。
String str = "PICC要写的文字";
ImageView image = (ImageView) this.findViewById(R.id.ImageView);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
int width = photo.getWidth(), hight = photo.getHeight();
System.out.println("宽"+width+"高"+hight);
icon = Bitmap.createBitmap(width, hight, Bitmap.Config.ARGB_8888); //建立一个空的BItMap
Canvas canvas = new Canvas(icon);//初始化画布绘制的图像到icon上
Paint photoPaint = new Paint(); //建立画笔
photoPaint.setDither(true); //获取跟清晰的图像采样
photoPaint.setFilterBitmap(true);//过滤一些
Rect src = new Rect(0, 0, photo.getWidth(), photo.getHeight());//创建一个指定的新矩形的坐标
Rect dst = new Rect(0, 0, width, hight);//创建一个指定的新矩形的坐标
canvas.drawBitmap(photo, src, dst, photoPaint);//将photo 缩放或则扩大到 dst使用的填充区photoPaint
Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);//设置画笔
textPaint.setTextSize(20.0f);//字体大小
textPaint.setTypeface(Typeface.DEFAULT_BOLD);//采用默认的宽度
textPaint.setColor(Color.RED);//采用的颜色
//textPaint.setShadowLayer(3f, 1, 1,this.getResources().getColor(android.R.color.background_dark));//影音的设置
canvas.drawText(str, 20, 26, textPaint);//绘制上去字,开始未知x,y采用那只笔绘制
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
image.setImageBitmap(icon);
saveMyBitmap(icon);
2. 圖片合併 : 在 Canvas上,使用 new Canvas( Bitmap1 ) 設定 Bitmap 為基底的畫布,然後將第二張圖使用 drawBitmap 畫至 Bitmap1。
Bitmap mark = BitmapFactory.decodeResource(this.getResources(), R.drawable.icon);
Bitmap photo = BitmapFactory.decodeResource(this.getResources(), R.drawable.text);
Bitmap a = createBitmap(photo,mark);
image.setImageBitmap(a);
saveMyBitmap(a);
private Bitmap createBitmap( Bitmap src, Bitmap watermark ){
String tag = "createBitmap";
// Log.d( tag, "create a new bitmap" );
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 );
//创建一个新的和SRC长度宽度一样的位图
Canvas cv = new Canvas( newb );
//draw src into
cv.drawBitmap( src, 0, 0, null );//在 0,0坐标开始画入src
//draw watermark into
cv.drawBitmap( watermark, w - ww + 5, h - wh + 5, null );//在src的右下角画入水印
//save all clip
cv.save( Canvas.ALL_SAVE_FLAG );//保存
//store
cv.restore();//存储
return newb;
}
參考資料:
- android图片加水印,文字 :http://www.eoeandroid.com/thread-225600-1-1.html
3. 圖片旋轉與放大 : 使用 Matrix 運算變形矩陣後,Bitmap.createBitmap 時將變形運算後的 Matrix 設定入參數中。
Matrix matrix = new Matrix();
matrix.postScale(scale, scale); // 設定縮放
matrix.setRotate(progress); // 設定旋轉角度
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true); // 設定 bitmap 長寬和 內容同形矩陣
myImageView.setImageBitmap(resizedBitmap); // 將 bitmap 放進 ImageView
參考資料:
- Android之UI学习篇六:ImageView实现图片旋转和缩放:http://blog.csdn.net/wulianghuan/article/details/8585990
- 利用Matrix類對位圖(bitmap)進行放大和縮小:http://androidbiancheng.blogspot.tw/2010/07/matrixbitmap.html
4、從資源中獲取Bitmap
Resources res = getResources();
Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
5、Bitmap → byte[]
public byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
6、byte[] → Bitmap
public Bitmap Bytes2Bimap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
7、Bitmap縮放
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newbmp;
}
6、獲得圓角圖片
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, w, h);
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 PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
8、獲得帶倒影的圖片
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, // 線性漸層效果
bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
0x00ffffff, TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
+ reflectionGap, paint);
return bitmapWithReflection;
}
參考資料:
- Android 颜色渲染(五) LinearGradient线性渲染 : http://blog.csdn.net/t12x3456/article/details/10566219
- Android平臺下實現漸變效果 : http://fecbob.pixnet.net/blog/post/35828189-android%E5%B9%B3%E8%87%BA%E4%B8%8B%E5%AF%A6%E7%8F%BE%E6%BC%B8%E8%AE%8A%E6%95%88%E6%9E%9C
-----------------------------
2. 儲存處理
-----------------------------
安卓BitmapFactory.decodeStream()返回null的问题解决方法:
Android Canvas 如何儲存成圖檔:http://tomkuo139.blogspot.tw/2010/03/android-canvas.html
沒有留言:
張貼留言