Image_utils

news/2024/11/16 13:30:01/
public class ImageUtil {


// 放大缩小图片
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidht = ((float) w / width);
float scaleHeight = ((float) h / height);
matrix.postScale(scaleWidht, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height,
matrix, true);
return newbmp;
}


// 将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(width, height, drawable
.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas);
return bitmap;


}


// 获得圆角图片的方法
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {


Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);


final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
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;
}


// 获得带倒影的图片方法
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGap = 4;
int width = bitmap.getWidth();
int height = bitmap.getHeight();


Matrix matrix = new Matrix();
matrix.preScale(1, -1);


Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
width, height / 2, matrix, false);


Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);


Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas
.drawRect(0, height, width, height + reflectionGap,
deafalutPaint);


canvas.drawBitmap(reflectionImage, 0, height + 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, height, width, bitmapWithReflection.getHeight()
+ reflectionGap, paint);


return bitmapWithReflection;
}


/**
* 边框 + 镜面反射渲染

* @param bitmap
* @return
*/
public static Bitmap createMirrorImageWithOrigain(Bitmap bitmap) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();


Bitmap bitmapWithReflection = Bitmap.createBitmap(w + 10, h + 10,
Config.ARGB_8888);


Canvas canvas = new Canvas(bitmapWithReflection);


Paint paint = new Paint();
paint.setColor(Color.DKGRAY);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(8);
canvas.drawRect(0, 0, w + 8, h + 8, paint);

paint.setColor(Color.GRAY);
paint.setStrokeWidth(2);
canvas.drawRect(0, 0, w + 10, h + 10, paint);




//光束渲染
//Shader radialGradient = new RadialGradient(10,10,3000,0x0000000f, 0xffffffff,TileMode.CLAMP);
//线性渲染
Shader linearGradient = new LinearGradient(0, 0, w*7/8, 0.01f,0x0000000f, 0xffffffff,
Shader.TileMode.MIRROR);
Shader linearGradient2 = new LinearGradient(0, 0, 0.01f, h,0x0000000f, 0xffffffff,
Shader.TileMode.MIRROR);
//混合渲染
Shader composeShader = new ComposeShader(linearGradient2,linearGradient,PorterDuff.Mode.SCREEN);


paint.reset();
paint.setShader(composeShader);
paint.setAlpha(0x30);


Path path = new Path();
path.moveTo(0, 0);
path.lineTo((w + 10) * 7 / 8, 0);
path.lineTo((w + 10) / 8, h + 10);
path.lineTo(0, h + 10);
path.close();
canvas.drawPath(path, paint);


canvas.drawBitmap(bitmap, 5, 5, null);

paint.reset();
paint.setColor(Color.WHITE);
paint.setAlpha(0x20);


Path path2 = new Path();
path2.moveTo(0, 0);
path2.lineTo((w + 10) * 7 / 8, 0);
path2.lineTo((w + 10) / 8, h + 10);
path2.lineTo(0, h + 10);
path2.close();
canvas.drawPath(path, paint);

return bitmapWithReflection;
}


}

http://www.ppmy.cn/news/572606.html

相关文章

解决ImageReady输出Gif颜色失真

GIF图片,在制作过程中,包括原图都没有出现问题,经常就是在优化另存为后或是直接点优化后,整个图片的颜色变色了,或者图片上出现斑斑点点,得到的gif图图色彩不如在ImageReady预览里的好看,这都是…

ImageView

ImageView,图像视图,直接继承自View类,它的主要功能是用于显示图片,实际上它不仅仅可以用来显示图片,任何Drawable对象都可以使用ImageView来显示。ImageView可以适用于任何布局中,并且Android为其提供了缩…

python-Image处理图片

使用python来处理图片是非常方便的,下面提供一小段python处理图片的代码,需要安装图像处理工具包PIL(Python Image Library)。 #codingutf-8import Image import urllib2 import StringIO import os#改变图片大小 def resize_img(img_path):try:img Im…

ImageData

/// The alignment of the terrain 地形的对齐模式enum Alignment//地形的对齐模式{/// Terrain is in the X/Z planeALIGN_X_Z 0, //地形的对其方式/// Terrain is in the X/Y planeALIGN_X_Y 1, /// Terrain is in the Y/Z planeALIGN_Y_Z 2};/** Structure encapsulating…

Image 图片

Image 图片 随机矩阵画图 这一节我们讲解怎样在matplotlib中打印出图像。这里打印出的是纯粹的数字,而非自然图像。下面用 3x3 的 2D-array 来表示点的颜色,每一个点就是一个pixel。 import matplotlib.pyplot as plt import numpy as npa np.array([0.…

image 读取 imagelist 里面图片

var i:integer1; procedure TForm4.BitBtn1Click(Sender: TObject); beginInc(i); if i>ImageList1.Count-1 then i:0;ImageList1.GetBitmap(i,image1.Picture.Bitmap) ; Self.Refresh; end;转载于:https://www.cnblogs.com/xh0626/p/5078289.html

imagematte

肖总博客:http://39.108.216.13:8090/display/~xiaozhenzhong/Image-MattingandBackgroundBlur 图像抠图的closed form算法讲解:http://blog.csdn.net/edesignerj/article/details/53349663 (本文用到的是input image和scribble image 其中 scribble im…