Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
mmm-rain
AndroidUtilCode
提交
27e246af
A
AndroidUtilCode
项目概览
mmm-rain
/
AndroidUtilCode
与 Fork 源项目一致
从无法访问的项目Fork
通知
4
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
AndroidUtilCode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
27e246af
编写于
9月 27, 2016
作者:
C
cmj
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
see 09/27 log
上级
4caff84f
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
44 addition
and
72 deletion
+44
-72
utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java
...e/src/main/java/com/blankj/utilcode/utils/ImageUtils.java
+44
-72
未找到文件。
utilcode/src/main/java/com/blankj/utilcode/utils/ImageUtils.java
浏览文件 @
27e246af
...
@@ -5,9 +5,7 @@ import android.content.Context;
...
@@ -5,9 +5,7 @@ import android.content.Context;
import
android.content.res.Resources
;
import
android.content.res.Resources
;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap
;
import
android.graphics.Bitmap.CompressFormat
;
import
android.graphics.Bitmap.CompressFormat
;
import
android.graphics.Bitmap.Config
;
import
android.graphics.BitmapFactory
;
import
android.graphics.BitmapFactory
;
import
android.graphics.BitmapShader
;
import
android.graphics.Canvas
;
import
android.graphics.Canvas
;
import
android.graphics.Color
;
import
android.graphics.Color
;
import
android.graphics.ColorMatrix
;
import
android.graphics.ColorMatrix
;
...
@@ -21,19 +19,17 @@ import android.graphics.PorterDuffXfermode;
...
@@ -21,19 +19,17 @@ import android.graphics.PorterDuffXfermode;
import
android.graphics.Rect
;
import
android.graphics.Rect
;
import
android.graphics.RectF
;
import
android.graphics.RectF
;
import
android.graphics.Shader
;
import
android.graphics.Shader
;
import
android.graphics.drawable.BitmapDrawable
;
import
android.graphics.drawable.Drawable
;
import
android.graphics.drawable.Drawable
;
import
android.hardware.camera2.params.BlackLevelPattern
;
import
android.media.ExifInterface
;
import
android.media.ExifInterface
;
import
android.os.Build
;
import
android.os.Build
;
import
android.renderscript.Allocation
;
import
android.renderscript.Allocation
;
import
android.renderscript.Element
;
import
android.renderscript.Element
;
import
android.renderscript.RenderScript
;
import
android.renderscript.RenderScript
;
import
android.renderscript.ScriptIntrinsicBlur
;
import
android.renderscript.ScriptIntrinsicBlur
;
import
android.view.View
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.BufferedOutputStream
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.File
;
import
java.io.FileDescriptor
;
import
java.io.FileDescriptor
;
...
@@ -43,7 +39,6 @@ import java.io.FileOutputStream;
...
@@ -43,7 +39,6 @@ import java.io.FileOutputStream;
import
java.io.IOException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStream
;
import
java.io.OutputStream
;
import
java.io.OutputStream
;
import
java.util.Date
;
/**
/**
* <pre>
* <pre>
...
@@ -74,7 +69,7 @@ public class ImageUtils {
...
@@ -74,7 +69,7 @@ public class ImageUtils {
* byteArr转bitmap
* byteArr转bitmap
*
*
* @param bytes 字节数组
* @param bytes 字节数组
* @return bitmap
对象
* @return bitmap
*/
*/
public
static
Bitmap
bytes2Bitmap
(
byte
[]
bytes
)
{
public
static
Bitmap
bytes2Bitmap
(
byte
[]
bytes
)
{
return
ConvertUtils
.
bytes2Bitmap
(
bytes
);
return
ConvertUtils
.
bytes2Bitmap
(
bytes
);
...
@@ -84,7 +79,7 @@ public class ImageUtils {
...
@@ -84,7 +79,7 @@ public class ImageUtils {
* drawable转bitmap
* drawable转bitmap
*
*
* @param drawable drawable对象
* @param drawable drawable对象
* @return bitmap
对象
* @return bitmap
*/
*/
public
static
Bitmap
drawable2Bitmap
(
Drawable
drawable
)
{
public
static
Bitmap
drawable2Bitmap
(
Drawable
drawable
)
{
return
ConvertUtils
.
drawable2Bitmap
(
drawable
);
return
ConvertUtils
.
drawable2Bitmap
(
drawable
);
...
@@ -95,7 +90,7 @@ public class ImageUtils {
...
@@ -95,7 +90,7 @@ public class ImageUtils {
*
*
* @param res resources对象
* @param res resources对象
* @param bitmap bitmap对象
* @param bitmap bitmap对象
* @return drawable
对象
* @return drawable
*/
*/
public
static
Drawable
bitmap2Drawable
(
Resources
res
,
Bitmap
bitmap
)
{
public
static
Drawable
bitmap2Drawable
(
Resources
res
,
Bitmap
bitmap
)
{
return
ConvertUtils
.
bitmap2Drawable
(
res
,
bitmap
);
return
ConvertUtils
.
bitmap2Drawable
(
res
,
bitmap
);
...
@@ -117,12 +112,22 @@ public class ImageUtils {
...
@@ -117,12 +112,22 @@ public class ImageUtils {
*
*
* @param res resources对象
* @param res resources对象
* @param bytes 字节数组
* @param bytes 字节数组
* @return drawable
对象
* @return drawable
*/
*/
public
static
Drawable
bytes2Drawable
(
Resources
res
,
byte
[]
bytes
)
{
public
static
Drawable
bytes2Drawable
(
Resources
res
,
byte
[]
bytes
)
{
return
ConvertUtils
.
bytes2Drawable
(
res
,
bytes
);
return
ConvertUtils
.
bytes2Drawable
(
res
,
bytes
);
}
}
/**
* view转Bitmap
*
* @param view 视图
* @return bitmap
*/
public
static
Bitmap
view2Bitmap
(
View
view
)
{
return
ConvertUtils
.
view2Bitmap
(
view
);
}
/**
/**
* 计算采样大小
* 计算采样大小
*
*
...
@@ -278,39 +283,6 @@ public class ImageUtils {
...
@@ -278,39 +283,6 @@ public class ImageUtils {
return
BitmapFactory
.
decodeByteArray
(
data
,
offset
,
data
.
length
,
options
);
return
BitmapFactory
.
decodeByteArray
(
data
,
offset
,
data
.
length
,
options
);
}
}
/**
* 获取bitmap
*
* @param context 上下文
* @param resId 资源id
* @return bitmap
*/
public
static
Bitmap
getBitmap
(
Context
context
,
int
resId
)
{
if
(
context
==
null
)
return
null
;
InputStream
is
=
context
.
getResources
().
openRawResource
(
resId
);
return
BitmapFactory
.
decodeStream
(
is
);
}
/**
* 获取bitmap
*
* @param context 上下文
* @param resId 资源id
* @param maxWidth 最大宽度
* @param maxHeight 最大高度
* @return bitmap
*/
public
static
Bitmap
getBitmap
(
Context
context
,
int
resId
,
int
maxWidth
,
int
maxHeight
)
{
if
(
context
==
null
)
return
null
;
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
();
options
.
inJustDecodeBounds
=
true
;
InputStream
is
=
context
.
getResources
().
openRawResource
(
resId
);
BitmapFactory
.
decodeStream
(
is
,
null
,
options
);
options
.
inSampleSize
=
calculateInSampleSize
(
options
,
maxWidth
,
maxHeight
);
options
.
inJustDecodeBounds
=
false
;
return
BitmapFactory
.
decodeStream
(
is
,
null
,
options
);
}
/**
/**
* 获取bitmap
* 获取bitmap
*
*
...
@@ -606,15 +578,14 @@ public class ImageUtils {
...
@@ -606,15 +578,14 @@ public class ImageUtils {
int
width
=
src
.
getWidth
();
int
width
=
src
.
getWidth
();
int
height
=
src
.
getHeight
();
int
height
=
src
.
getHeight
();
int
radius
=
Math
.
min
(
width
,
height
)
>>
1
;
int
radius
=
Math
.
min
(
width
,
height
)
>>
1
;
Bitmap
ret
=
src
.
copy
(
src
.
getConfig
(),
true
);
Bitmap
ret
=
Bitmap
.
createBitmap
(
width
,
height
,
src
.
getConfig
()
);
Paint
paint
=
new
Paint
();
Paint
paint
=
new
Paint
();
Canvas
canvas
=
new
Canvas
(
ret
);
Canvas
canvas
=
new
Canvas
(
ret
);
Rect
rect
=
new
Rect
(
0
,
0
,
width
,
height
);
Rect
rect
=
new
Rect
(
0
,
0
,
width
,
height
);
paint
.
setAntiAlias
(
true
);
paint
.
setAntiAlias
(
true
);
paint
.
setColor
(
Color
.
TRANSPARENT
);
paint
.
setXfermode
(
new
PorterDuffXfermode
(
PorterDuff
.
Mode
.
SRC_IN
));
canvas
.
drawARGB
(
0
,
0
,
0
,
0
);
canvas
.
drawARGB
(
0
,
0
,
0
,
0
);
canvas
.
drawCircle
(
width
>>
1
,
height
>>
1
,
radius
,
paint
);
canvas
.
drawCircle
(
width
>>
1
,
height
>>
1
,
radius
,
paint
);
paint
.
setXfermode
(
new
PorterDuffXfermode
(
PorterDuff
.
Mode
.
SRC_IN
));
canvas
.
drawBitmap
(
src
,
rect
,
rect
,
paint
);
canvas
.
drawBitmap
(
src
,
rect
,
rect
,
paint
);
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
return
ret
;
return
ret
;
...
@@ -643,15 +614,15 @@ public class ImageUtils {
...
@@ -643,15 +614,15 @@ public class ImageUtils {
if
(
null
==
src
)
return
null
;
if
(
null
==
src
)
return
null
;
int
width
=
src
.
getWidth
();
int
width
=
src
.
getWidth
();
int
height
=
src
.
getHeight
();
int
height
=
src
.
getHeight
();
Bitmap
ret
=
src
.
copy
(
src
.
getConfig
(),
true
);
Bitmap
ret
=
Bitmap
.
createBitmap
(
width
,
height
,
src
.
getConfig
());
BitmapShader
bitmapShader
=
new
BitmapShader
(
src
,
Shader
.
TileMode
.
CLAMP
,
Shader
.
TileMode
.
CLAMP
);
Paint
paint
=
new
Paint
();
Paint
paint
=
new
Paint
();
Canvas
canvas
=
new
Canvas
(
ret
);
Canvas
canvas
=
new
Canvas
(
ret
);
Rect
F
rectf
=
new
RectF
(
0
,
0
,
width
,
height
);
Rect
rect
=
new
Rect
(
0
,
0
,
width
,
height
);
paint
.
setAntiAlias
(
true
);
paint
.
setAntiAlias
(
true
);
paint
.
setShader
(
bitmapShader
);
canvas
.
drawARGB
(
0
,
0
,
0
,
0
);
canvas
.
drawRoundRect
(
rectf
,
radius
,
radius
,
paint
);
canvas
.
drawRoundRect
(
new
RectF
(
rect
),
radius
,
radius
,
paint
);
paint
.
setXfermode
(
new
PorterDuffXfermode
(
PorterDuff
.
Mode
.
SRC_IN
));
canvas
.
drawBitmap
(
src
,
rect
,
rect
,
paint
);
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
return
ret
;
return
ret
;
}
}
...
@@ -671,7 +642,7 @@ public class ImageUtils {
...
@@ -671,7 +642,7 @@ public class ImageUtils {
}
}
/**
/**
* 快速模糊
* 快速模糊
图片
* <p>先缩小原图,对小图进行模糊,再放大回原先尺寸</p>
* <p>先缩小原图,对小图进行模糊,再放大回原先尺寸</p>
*
*
* @param context 上下文
* @param context 上下文
...
@@ -699,7 +670,7 @@ public class ImageUtils {
...
@@ -699,7 +670,7 @@ public class ImageUtils {
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
if
(
Build
.
VERSION
.
SDK_INT
>=
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
{
scaleBitmap
=
renderScriptBlur
(
context
,
scaleBitmap
,
radius
);
scaleBitmap
=
renderScriptBlur
(
context
,
scaleBitmap
,
radius
);
}
else
{
}
else
{
scaleBitmap
=
stackBlur
(
scaleBitmap
,
(
int
)
radius
,
tru
e
);
scaleBitmap
=
stackBlur
(
scaleBitmap
,
(
int
)
radius
,
recycl
e
);
}
}
if
(
scale
==
1
)
return
scaleBitmap
;
if
(
scale
==
1
)
return
scaleBitmap
;
Bitmap
ret
=
Bitmap
.
createScaledBitmap
(
scaleBitmap
,
width
,
height
,
true
);
Bitmap
ret
=
Bitmap
.
createScaledBitmap
(
scaleBitmap
,
width
,
height
,
true
);
...
@@ -714,7 +685,7 @@ public class ImageUtils {
...
@@ -714,7 +685,7 @@ public class ImageUtils {
*
*
* @param context 上下文
* @param context 上下文
* @param src 源图片
* @param src 源图片
* @param radius 模糊度(
0
...25)
* @param radius 模糊度(
1
...25)
* @return 模糊后的图片
* @return 模糊后的图片
*/
*/
@TargetApi
(
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
@TargetApi
(
Build
.
VERSION_CODES
.
JELLY_BEAN_MR1
)
...
@@ -751,7 +722,7 @@ public class ImageUtils {
...
@@ -751,7 +722,7 @@ public class ImageUtils {
* @param src 源图片
* @param src 源图片
* @param radius 模糊半径
* @param radius 模糊半径
* @param recycle 是否回收
* @param recycle 是否回收
* @return stack
Blur模糊
图片
* @return stack
模糊后的
图片
*/
*/
public
static
Bitmap
stackBlur
(
Bitmap
src
,
int
radius
,
boolean
recycle
)
{
public
static
Bitmap
stackBlur
(
Bitmap
src
,
int
radius
,
boolean
recycle
)
{
Bitmap
ret
;
Bitmap
ret
;
...
@@ -762,7 +733,7 @@ public class ImageUtils {
...
@@ -762,7 +733,7 @@ public class ImageUtils {
}
}
if
(
radius
<
1
)
{
if
(
radius
<
1
)
{
return
(
null
)
;
return
null
;
}
}
int
w
=
ret
.
getWidth
();
int
w
=
ret
.
getWidth
();
...
@@ -954,7 +925,7 @@ public class ImageUtils {
...
@@ -954,7 +925,7 @@ public class ImageUtils {
}
}
}
}
ret
.
setPixels
(
pix
,
0
,
w
,
0
,
0
,
w
,
h
);
ret
.
setPixels
(
pix
,
0
,
w
,
0
,
0
,
w
,
h
);
return
(
ret
)
;
return
ret
;
}
}
/**
/**
...
@@ -966,7 +937,7 @@ public class ImageUtils {
...
@@ -966,7 +937,7 @@ public class ImageUtils {
* @return 带颜色边框图
* @return 带颜色边框图
*/
*/
public
static
Bitmap
addFrame
(
Bitmap
src
,
int
borderWidth
,
int
color
)
{
public
static
Bitmap
addFrame
(
Bitmap
src
,
int
borderWidth
,
int
color
)
{
return
addFrame
(
src
,
borderWidth
,
color
);
return
addFrame
(
src
,
borderWidth
,
color
,
false
);
}
}
/**
/**
...
@@ -980,16 +951,18 @@ public class ImageUtils {
...
@@ -980,16 +951,18 @@ public class ImageUtils {
*/
*/
public
static
Bitmap
addFrame
(
Bitmap
src
,
int
borderWidth
,
int
color
,
boolean
recycle
)
{
public
static
Bitmap
addFrame
(
Bitmap
src
,
int
borderWidth
,
int
color
,
boolean
recycle
)
{
if
(
isEmptyBitmap
(
src
))
return
null
;
if
(
isEmptyBitmap
(
src
))
return
null
;
int
newWidth
=
src
.
getWidth
()
+
borderWidth
>>
1
;
int
doubleBorder
=
borderWidth
<<
1
;
int
newHeight
=
src
.
getHeight
()
+
borderWidth
>>
1
;
int
newWidth
=
src
.
getWidth
()
+
doubleBorder
;
int
newHeight
=
src
.
getHeight
()
+
doubleBorder
;
Bitmap
ret
=
Bitmap
.
createBitmap
(
newWidth
,
newHeight
,
src
.
getConfig
());
Bitmap
ret
=
Bitmap
.
createBitmap
(
newWidth
,
newHeight
,
src
.
getConfig
());
Canvas
canvas
=
new
Canvas
(
ret
);
Canvas
canvas
=
new
Canvas
(
ret
);
Rect
rec
=
canvas
.
getClipBounds
(
);
Rect
rec
t
=
new
Rect
(
0
,
0
,
newWidth
,
newHeight
);
Paint
paint
=
new
Paint
();
Paint
paint
=
new
Paint
();
paint
.
setColor
(
color
);
paint
.
setColor
(
color
);
paint
.
setStyle
(
Paint
.
Style
.
STROKE
);
paint
.
setStyle
(
Paint
.
Style
.
STROKE
);
paint
.
setStrokeWidth
(
borderWidth
);
// setStrokeWidth是居中画的,所以要两倍的宽度才能画,否则有一半的宽度是空的
canvas
.
drawRect
(
rec
,
paint
);
paint
.
setStrokeWidth
(
doubleBorder
);
canvas
.
drawRect
(
rect
,
paint
);
canvas
.
drawBitmap
(
src
,
borderWidth
,
borderWidth
,
null
);
canvas
.
drawBitmap
(
src
,
borderWidth
,
borderWidth
,
null
);
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
return
ret
;
return
ret
;
...
@@ -1053,13 +1026,13 @@ public class ImageUtils {
...
@@ -1053,13 +1026,13 @@ public class ImageUtils {
* @param content 水印文本
* @param content 水印文本
* @param textSize 水印字体大小
* @param textSize 水印字体大小
* @param color 水印字体颜色
* @param color 水印字体颜色
* @param alpha 水印字体透明度
* @param x 起始坐标x
* @param x 起始坐标x
* @param y 起始坐标y
* @param y 起始坐标y
* @return 带有文字水印的图片
* @return 带有文字水印的图片
*/
*/
public
static
Bitmap
addTextWatermark
(
Bitmap
src
,
String
content
,
int
textSize
,
int
color
,
int
alpha
,
float
x
,
float
y
)
{
public
static
Bitmap
addTextWatermark
(
Bitmap
src
,
String
content
,
int
textSize
,
int
color
,
float
x
,
return
addTextWatermark
(
src
,
content
,
textSize
,
color
,
alpha
,
x
,
y
,
false
);
float
y
)
{
return
addTextWatermark
(
src
,
content
,
textSize
,
color
,
x
,
y
,
false
);
}
}
/**
/**
...
@@ -1069,23 +1042,22 @@ public class ImageUtils {
...
@@ -1069,23 +1042,22 @@ public class ImageUtils {
* @param content 水印文本
* @param content 水印文本
* @param textSize 水印字体大小
* @param textSize 水印字体大小
* @param color 水印字体颜色
* @param color 水印字体颜色
* @param alpha 水印字体透明度
* @param x 起始坐标x
* @param x 起始坐标x
* @param y 起始坐标y
* @param y 起始坐标y
* @param recycle 是否回收
* @param recycle 是否回收
* @return 带有文字水印的图片
* @return 带有文字水印的图片
*/
*/
public
static
Bitmap
addTextWatermark
(
Bitmap
src
,
String
content
,
int
textSize
,
int
color
,
int
alpha
,
float
x
,
float
y
,
boolean
recycle
)
{
public
static
Bitmap
addTextWatermark
(
Bitmap
src
,
String
content
,
float
textSize
,
int
color
,
float
x
,
float
y
,
boolean
recycle
)
{
if
(
isEmptyBitmap
(
src
)
||
content
==
null
)
return
null
;
if
(
isEmptyBitmap
(
src
)
||
content
==
null
)
return
null
;
Bitmap
ret
=
src
.
copy
(
src
.
getConfig
(),
true
);
Bitmap
ret
=
src
.
copy
(
src
.
getConfig
(),
true
);
Paint
paint
=
new
Paint
(
Paint
.
ANTI_ALIAS_FLAG
);
Paint
paint
=
new
Paint
(
Paint
.
ANTI_ALIAS_FLAG
);
Canvas
canvas
=
new
Canvas
(
ret
);
Canvas
canvas
=
new
Canvas
(
ret
);
paint
.
setAlpha
(
alpha
);
paint
.
setColor
(
color
);
paint
.
setColor
(
color
);
paint
.
setTextSize
(
textSize
);
paint
.
setTextSize
(
textSize
);
Rect
bounds
=
new
Rect
();
Rect
bounds
=
new
Rect
();
paint
.
getTextBounds
(
content
,
0
,
content
.
length
(),
bounds
);
paint
.
getTextBounds
(
content
,
0
,
content
.
length
(),
bounds
);
canvas
.
drawText
(
content
,
x
,
y
,
paint
);
canvas
.
drawText
(
content
,
x
,
y
+
textSize
,
paint
);
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
if
(
recycle
&&
!
src
.
isRecycled
())
src
.
recycle
();
return
ret
;
return
ret
;
}
}
...
@@ -1135,7 +1107,7 @@ public class ImageUtils {
...
@@ -1135,7 +1107,7 @@ public class ImageUtils {
* @return alpha位图
* @return alpha位图
*/
*/
public
static
Bitmap
toAlpha
(
Bitmap
src
)
{
public
static
Bitmap
toAlpha
(
Bitmap
src
)
{
return
toAlpha
(
src
);
return
toAlpha
(
src
,
false
);
}
}
/**
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录