Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-fluwx
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
clx-fluwx
Commits
14b6800f
提交
14b6800f
authored
8月 21, 2018
作者:
JarvanMo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
android压片算法优化
上级
75552677
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
40 行增加
和
15 行删除
+40
-15
ThumbnailCompressUtil.java
.../kotlin/com/jarvan/fluwx/utils/ThumbnailCompressUtil.java
+28
-0
WeChatThumbnailUtil.java
...in/kotlin/com/jarvan/fluwx/utils/WeChatThumbnailUtil.java
+12
-15
没有找到文件。
android/src/main/kotlin/com/jarvan/fluwx/utils/ThumbnailCompressUtil.java
浏览文件 @
14b6800f
...
...
@@ -155,4 +155,32 @@ public class ThumbnailCompressUtil {
return
thumb
;
}
public
static
Bitmap
createScaledBitmapWithRatio
(
Bitmap
bitmap
,
int
maxLength
,
boolean
recycle
){
Bitmap
result
=
bitmap
;
while
(
true
){
double
ratio
=((
double
)
maxLength
)/
result
.
getByteCount
();
double
width
=
result
.
getWidth
()
*
Math
.
sqrt
(
ratio
);
double
height
=
result
.
getHeight
()
*
Math
.
sqrt
(
ratio
);
Bitmap
tmp
=
Bitmap
.
createScaledBitmap
(
result
,
(
int
)
width
,
(
int
)
height
,
true
);
if
(
result
!=
bitmap
){
result
.
recycle
();
}
result
=
tmp
;
if
(
result
.
getByteCount
()
<
maxLength
)
{
break
;
}
}
if
(
recycle
)
{
bitmap
.
recycle
();
}
return
result
;
}
}
android/src/main/kotlin/com/jarvan/fluwx/utils/WeChatThumbnailUtil.java
浏览文件 @
14b6800f
...
...
@@ -30,9 +30,8 @@ import okio.Source;
import
top.zibin.luban.Luban
;
public
class
WeChatThumbnailUtil
{
public
static
final
int
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
=
120
;
public
static
final
int
MINI_PROGRAM_SCALED_WIDTH
=
480
;
public
static
final
int
SHARE_IMAGE_THUMB_LENGTH
=
32
;
public
static
final
int
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
=
120
*
1024
;
public
static
final
int
SHARE_IMAGE_THUMB_LENGTH
=
32
*
1024
;
private
static
final
int
COMMON_THUMB_WIDTH
=
150
;
private
WeChatThumbnailUtil
()
{
...
...
@@ -47,7 +46,7 @@ public class WeChatThumbnailUtil {
}
else
{
file
=
downloadImage
(
thumbnail
);
}
return
compress
(
file
,
registrar
,
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
,
MINI_PROGRAM_SCALED_WIDTH
);
return
compress
(
file
,
registrar
,
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
);
}
...
...
@@ -60,7 +59,7 @@ public class WeChatThumbnailUtil {
}
else
{
file
=
downloadImage
(
thumbnail
);
}
return
compress
(
file
,
registrar
,
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
,
MINI_PROGRAM_SCALED_WIDTH
);
return
compress
(
file
,
registrar
,
SHARE_MINI_PROGRAM_IMAGE_THUMB_LENGTH
);
}
public
static
byte
[]
thumbnailForCommon
(
String
thumbnail
,
PluginRegistry
.
Registrar
registrar
)
{
...
...
@@ -72,10 +71,10 @@ public class WeChatThumbnailUtil {
}
else
{
file
=
downloadImage
(
thumbnail
);
}
return
compress
(
file
,
registrar
,
SHARE_IMAGE_THUMB_LENGTH
,
COMMON_THUMB_WIDTH
);
return
compress
(
file
,
registrar
,
SHARE_IMAGE_THUMB_LENGTH
);
}
private
static
byte
[]
compress
(
File
file
,
PluginRegistry
.
Registrar
registrar
,
int
resultMaxLength
,
int
scaledWidth
)
{
private
static
byte
[]
compress
(
File
file
,
PluginRegistry
.
Registrar
registrar
,
int
resultMaxLength
)
{
if
(
file
==
null
)
{
return
new
byte
[]{};
}
...
...
@@ -87,7 +86,7 @@ public class WeChatThumbnailUtil {
.
ignoreBy
(
resultMaxLength
)
.
setTargetDir
(
registrar
.
context
().
getCacheDir
().
getAbsolutePath
())
.
get
(
file
.
getAbsolutePath
());
if
(
compressedFile
.
length
()
<
resultMaxLength
*
1024
)
{
if
(
compressedFile
.
length
()
<
resultMaxLength
)
{
Source
source
=
Okio
.
source
(
compressedFile
);
BufferedSource
bufferedSource
=
Okio
.
buffer
(
source
);
byte
[]
bytes
=
bufferedSource
.
readByteArray
();
...
...
@@ -95,12 +94,10 @@ public class WeChatThumbnailUtil {
bufferedSource
.
close
();
return
bytes
;
}
byte
[]
result
=
createScaledBitmapWithRatio
(
compressedFile
,
scaledWidth
);
if
(
result
.
length
<
resultMaxLength
*
1024
)
{
return
result
;
}
return
createScaledBitmapWithRatio
(
compressedFile
,
resultMaxLength
);
return
createScaledBitmap
(
compressedFile
,
resultMaxLength
,
scaledWidth
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
...
...
@@ -108,10 +105,10 @@ public class WeChatThumbnailUtil {
return
new
byte
[]{};
}
private
static
byte
[]
createScaledBitmapWithRatio
(
File
file
,
int
scaledWid
th
)
{
private
static
byte
[]
createScaledBitmapWithRatio
(
File
file
,
int
resultMaxLeng
th
)
{
Bitmap
originBitmap
=
BitmapFactory
.
decodeFile
(
file
.
getAbsolutePath
());
Bitmap
result
=
ThumbnailCompressUtil
.
createScaledBitmap
WithRatio
(
originBitmap
,
scaledWid
th
,
true
);
Bitmap
result
=
ThumbnailCompressUtil
.
createScaledBitmap
(
originBitmap
,
resultMaxLeng
th
,
true
);
String
path
=
file
.
getAbsolutePath
();
String
suffix
=
path
.
substring
(
path
.
lastIndexOf
(
"."
),
path
.
length
());
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论