/*** 将2张图片合成* @param downBitmap 底部图片* @param upBitmap 置顶的图片* @return*/public Bitmap compoundBitmap(Bitmap downBitmap,Bitmap upBitmap){if(downBitmap == null || upBitmap == null) {return null;}Bitmap mBitmap = downBitmap.copy(Bitmap.Config.ARGB_8888, true);Bitmap upNewBitmap = upBitmap;//如果遇到黑色,则显示downBitmap里面的颜色值,如果不是则显示upBitmap里面的颜色值//循环获得bitmap所有像素点int mBitmapWidth = mBitmap.getWidth();int mBitmapHeight = mBitmap.getHeight();//首先保证downBitmap和 upBitmap是一致的高宽大小if (DEBUG) {Log.d("may", "mBitmapWidth=" + mBitmapWidth + ",upBitmap.getWidth()=" + upBitmap.getWidth());Log.d("may", "mBitmapHeight=" + mBitmapHeight + ",upBitmap.getHeight()=" + upBitmap.getHeight());}if(mBitmapWidth != upBitmap.getWidth() || mBitmapHeight != upBitmap.getHeight()) {upNewBitmap = resizeImage(upBitmap, mBitmapWidth, mBitmapHeight);}if(mBitmapWidth==upBitmap.getWidth() && mBitmapHeight==upBitmap.getHeight()){if (DEBUG)Log.d("may", "compoundBitmap mBitmapWidth==upBitmap.getWidth()");for (int i = 0; i < mBitmapHeight; i++) {for (int j = 0; j < mBitmapWidth; j++) {//获得Bitmap 图片中每一个点的color颜色值//将需要填充的颜色值如果不是//在这说明一下 如果color 是全透明 或者全黑 返回值为 0//getPixel()不带透明通道 getPixel32()才带透明部分 所以全透明是0x00000000//而不透明黑色是0xFF000000 如果不计算透明部分就都是0了int color = upNewBitmap.getPixel(j, i);//将颜色值存在一个数组中 方便后面修改if (color != Color.BLACK ) {if(color != 0x00000000 ) {mBitmap.setPixel(j, i, upNewBitmap.getPixel(j, i));}} else {mBitmap.setPixel(j, i, 0x00000000);}}}}
// downBitmap.recycle();
// upBitmap.recycle();return mBitmap;}//使用Bitmap加Matrix来缩放public Bitmap resizeImage(Bitmap bitmap, int w, int h) {Bitmap BitmapOrg = bitmap;int width = BitmapOrg.getWidth();int height = BitmapOrg.getHeight();int newWidth = w;int newHeight = h;float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// if you want to rotate the Bitmap// matrix.postRotate(45);Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,height, matrix, true);//return new BitmapDrawable(resizedBitmap);return resizedBitmap;}/*两张图片合成一张图片*/private Bitmap mergeBitmap(Bitmap firstBitmap, Bitmap secondBitmap) {Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(),firstBitmap.getConfig());Canvas canvas = new Canvas(bitmap);canvas.drawBitmap(firstBitmap, new Matrix(), null);// canvas.drawBitmap(firstBitmap,0, 0,null);canvas.drawBitmap(secondBitmap, 0, 0, null);return bitmap;}private Bitmap mergePhoto(Bitmap firstBitmap, Bitmap secondBitmap) {// 防止出现Immutable bitmap passed to Canvas constructor错误
// Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(),
// R.drawable.apple).copy(Bitmap.Config.ARGB_8888, true);
// Bitmap bitmap2 = ((BitmapDrawable) getResources().getDrawable(
// R.drawable.go)).getBitmap();Bitmap bitmap = Bitmap.createBitmap(firstBitmap.getWidth(), firstBitmap.getHeight(),firstBitmap.getConfig());Bitmap newBitmap = null;newBitmap = Bitmap.createBitmap(bitmap);Canvas canvas = new Canvas(newBitmap);Paint paint = new Paint();int w = firstBitmap.getWidth();int h = firstBitmap.getHeight();int w_2 = secondBitmap.getWidth();int h_2 = secondBitmap.getHeight();paint.setColor(Color.GRAY);paint.setAlpha(125);canvas.drawRect(0, 0, firstBitmap.getWidth(), firstBitmap.getHeight(), paint);paint = new Paint();canvas.drawBitmap(secondBitmap, Math.abs(w - w_2) / 2,Math.abs(h - h_2) / 2, paint);canvas.save(Canvas.ALL_SAVE_FLAG);// 存储新合成的图片canvas.restore();return newBitmap;}/*从资源中获取的Drawable --> BitmapmContext*/private Bitmap drawbleChangetoBitmap(int redId, Context context) {Resources res = context.getResources();Bitmap bmp = BitmapFactory.decodeResource(res, redId);return bmp;}/*** 把图片转成圆形图片* 这个暂时没有用* @param bitmap* @return*/private Bitmap getRoundedCornerBitmap(Bitmap bitmap) {if(bitmap == null) {return null;}Bitmap roundBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(roundBitmap);int color = 0xff424242;Paint paint = new Paint();Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());RectF rectF = new RectF(rect);float roundPx = 50;paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(bitmap, rect, rect, paint);return roundBitmap;}/*** 转换图片成圆形* @param bitmap 传入Bitmap对象* @return*/public static Bitmap toRoundBitmap(Bitmap bitmap) {if(bitmap == null) {return null;}int width = bitmap.getWidth();int height = bitmap.getHeight();float roundPx;float left,top,right,bottom,dst_left,dst_top,dst_right,dst_bottom;if (width <= height) {roundPx = width / 2;top = 0;bottom = width;left = 0;right = width;height = width;dst_left = 0;dst_top = 0;dst_right = width;dst_bottom = width;} else {roundPx = height / 2;float clip = (width - height) / 2;left = clip;right = width - clip;top = 0;bottom = height;width = height;dst_left = 0;dst_top = 0;dst_right = height;dst_bottom = height;}Bitmap output = Bitmap.createBitmap(width,height, Bitmap.Config.ARGB_8888);if(output == null) {return null;}Canvas canvas = new Canvas(output);// final int color = 0xff424242;final Paint paint = new Paint();final Rect src = new Rect((int)left, (int)top, (int)right, (int)bottom);final Rect dst = new Rect((int)dst_left, (int)dst_top, (int)dst_right, (int)dst_bottom);final RectF rectF = new RectF(dst);paint.setAntiAlias(true);canvas.drawARGB(0, 0, 0, 0);// paint.setColor(color);canvas.drawRoundRect(rectF, roundPx, roundPx, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));canvas.drawBitmap(bitmap, src, dst, paint);return output;}private static int mRoundedRate = 10;/*** 1.做圆角处理(椭圆)* 2.增加背景色* @param src* @param iconSize* @return*/private static Bitmap addBorderToImage(Bitmap src, int iconSize) {if(src == null) {return null;}int width = iconSize;//getBorderWidth();int height = iconSize;//getBorderHeight();Bitmap output = Bitmap.createBitmap(width, height,Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(output);RectF outerRect = new RectF(0,0,width,height);Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.FILTER_BITMAP_FLAG);LinearGradient mLinearGradientClamp = new LinearGradient(0, 0, 0, height, new int[] {getCenterTopColor(src),getCenterBottomColor(src)}, null, Shader.TileMode.CLAMP);paint.setShader(mLinearGradientClamp);canvas.drawRoundRect(outerRect, width/mRoundedRate, height/mRoundedRate, paint);paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));int sw = src.getWidth();int sh = src.getHeight();Rect srcRect = new Rect(0,0,sw,sh);Rect dstRect = getDstRect(sw,sh,width,height);canvas.drawBitmap(src, srcRect, dstRect, paint);return output;}/*** 1.填充背景色* 2.转换成圆形* @param src* @param iconSize* @return*/public static Bitmap addBorderToRoundImage(Bitmap src, int iconSize) {Bitmap icon = addBorderToImage(src, iconSize);return toRoundBitmap(icon);}private static Rect getDstRect( int sw,int sh,int w,int h){return new Rect(0,0,w,h);}/*** 得到原始图片中,底部中间的第一个完全不透明的颜色* @return*/private static int getCenterTopColor(Bitmap src){int w = src.getWidth();int h = src.getHeight();int x = (int) (w*0.5);int c = Color.WHITE;for(int y =h-1;y>=0;--y){int pixel = src.getPixel(x, y);if(Color.alpha(pixel)==0xFF){c = pixel;}}return c;
// return softColor(c);}/*** 得到原始图片中,顶部中间的第一个完全不透明的颜色* @return*/private static int getCenterBottomColor(Bitmap src){int w = src.getWidth();int h = src.getHeight();int x = (int) (w*0.5);int c = Color.WHITE;for(int y =0;y<h;++y){int pixel = src.getPixel(x, y);if(Color.alpha(pixel)==0xFF){c = pixel;}}return c;
// return softColor(c);}/*** 将颜色变浅,变淡 一旦使用此方法,边框颜色会对应不上,故上面两个方法没有使用* @param color* @return*/private static int softColor(int color){int r = (int) (Color.red(color)*1.3);int b = (int) (Color.blue(color)*1.3);int g = (int) (Color.green(color)*1.3);int af = 0xFF;if(b >= 0xFF){b = 0xFF;}if(r>=0xFF){r = 0xFF;}if(g>=0xFF){g = 0xFF;}return af<<24|r<<16|g<<8|b;}
详细解决方案
图片合成
热度:66 发布时间:2024-01-24 11:04:08.0
相关解决方案