Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
clx-fluwx
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
openSourceLibrary
clx-fluwx
Commits
20a7d2e2
提交
20a7d2e2
authored
8月 12, 2018
作者:
JarvanMo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
share Image
上级
d76d932b
隐藏空白字符变更
内嵌
并排
正在显示
15 个修改的文件
包含
601 行增加
和
57 行删除
+601
-57
build.gradle
android/build.gradle
+4
-3
AppRegister.java
...rc/main/kotlin/com/jarvanmo/wechatplugin/AppRegister.java
+0
-1
WechatPlugin.kt
...src/main/kotlin/com/jarvanmo/wechatplugin/WechatPlugin.kt
+8
-2
WeChatPluginImageSchema.java
...jarvanmo/wechatplugin/config/WeChatPluginImageSchema.java
+8
-0
WechatPluginConfig.java
.../com/jarvanmo/wechatplugin/config/WechatPluginConfig.java
+2
-0
WeChatPluginHandler.kt
.../com/jarvanmo/wechatplugin/handler/WeChatPluginHandler.kt
+84
-25
AssetManagerUtil.java
...lin/com/jarvanmo/wechatplugin/utils/AssetManagerUtil.java
+26
-0
CompressImageUtil.java
...in/com/jarvanmo/wechatplugin/utils/CompressImageUtil.java
+45
-0
FileUtil.java
...main/kotlin/com/jarvanmo/wechatplugin/utils/FileUtil.java
+60
-0
Util.java
...src/main/kotlin/com/jarvanmo/wechatplugin/utils/Util.java
+203
-0
WeChatThumbnailUtil.java
.../com/jarvanmo/wechatplugin/utils/WeChatThumbnailUtil.java
+112
-0
build.gradle
example/android/build.gradle
+1
-1
main.dart
example/lib/main.dart
+1
-0
wechat_share_models.dart
lib/src/wechat_share_models.dart
+46
-24
wechat_plugin.dart
lib/wechat_plugin.dart
+1
-1
没有找到文件。
android/build.gradle
浏览文件 @
20a7d2e2
...
@@ -2,7 +2,7 @@ group 'com.jarvanmo.wechatplugin'
...
@@ -2,7 +2,7 @@ group 'com.jarvanmo.wechatplugin'
version
'1.0-SNAPSHOT'
version
'1.0-SNAPSHOT'
buildscript
{
buildscript
{
ext
.
kotlin_version
=
'1.2.
51
'
ext
.
kotlin_version
=
'1.2.
60
'
repositories
{
repositories
{
google
()
google
()
jcenter
()
jcenter
()
...
@@ -40,10 +40,11 @@ android {
...
@@ -40,10 +40,11 @@ android {
}
}
dependencies
{
dependencies
{
implementation
"org.jetbrains.kotlin:kotlin-stdlib-jdk:$kotlin_version"
implementation
"org.jetbrains.kotlin:kotlin-stdlib-jdk
7
:$kotlin_version"
implementation
'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
implementation
'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+'
implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0'
implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-core:0.24.0'
implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-common:0.24.0'
//
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-common:0.24.0'
implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0'
implementation
'org.jetbrains.kotlinx:kotlinx-coroutines-android:0.24.0'
implementation
'top.zibin:Luban:1.1.8'
implementation
'top.zibin:Luban:1.1.8'
implementation
'com.squareup.okhttp3:okhttp:3.11.0'
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/AppRegister.java
浏览文件 @
20a7d2e2
...
@@ -15,6 +15,5 @@ public class AppRegister extends BroadcastReceiver {
...
@@ -15,6 +15,5 @@ public class AppRegister extends BroadcastReceiver {
final
IWXAPI
api
=
WXAPIFactory
.
createWXAPI
(
context
,
null
);
final
IWXAPI
api
=
WXAPIFactory
.
createWXAPI
(
context
,
null
);
// 将该app注册到微信
// 将该app注册到微信
api
.
registerApp
(
WechatPluginConfig
.
appId
);
api
.
registerApp
(
WechatPluginConfig
.
appId
);
}
}
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/WechatPlugin.kt
浏览文件 @
20a7d2e2
...
@@ -11,10 +11,14 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
...
@@ -11,10 +11,14 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
class
WechatPlugin
(
private
var
channel
:
MethodChannel
,
private
var
registrar
:
Registrar
)
:
MethodCallHandler
{
class
WechatPlugin
(
private
var
channel
:
MethodChannel
,
private
var
registrar
:
Registrar
)
:
MethodCallHandler
{
companion
object
{
companion
object
{
@JvmStatic
val
TAG
=
"WeChatPlugin"
val
FILE_NOT_EXIST
=
"file not exists"
@JvmStatic
@JvmStatic
fun
registerWith
(
registrar
:
Registrar
):
Unit
{
fun
registerWith
(
registrar
:
Registrar
):
Unit
{
val
channel
=
MethodChannel
(
registrar
.
messenger
(),
"wechat_plugin"
)
val
channel
=
MethodChannel
(
registrar
.
messenger
(),
"wechat_plugin"
)
WeChatPluginHandler
.
set
Context
(
registrar
.
context
().
applicationContext
)
WeChatPluginHandler
.
set
Registrar
(
registrar
)
channel
.
setMethodCallHandler
(
WechatPlugin
(
channel
,
registrar
))
channel
.
setMethodCallHandler
(
WechatPlugin
(
channel
,
registrar
))
WeChatPluginHandler
.
setMethodChannel
(
channel
)
WeChatPluginHandler
.
setMethodChannel
(
channel
)
}
}
...
@@ -31,7 +35,9 @@ class WechatPlugin(private var channel: MethodChannel,private var registrar: Reg
...
@@ -31,7 +35,9 @@ class WechatPlugin(private var channel: MethodChannel,private var registrar: Reg
result
.
error
(
"config your wxapi first"
,
"config your wxapi first"
,
null
)
result
.
error
(
"config your wxapi first"
,
"config your wxapi first"
,
null
)
return
return
}
}
else
->
WeChatPluginHandler
.
handle
(
call
,
result
)
call
.
method
.
startsWith
(
"share"
)->{
WeChatPluginHandler
.
handle
(
call
,
result
)
}
}
}
...
...
android/src/main/kotlin/com/jarvanmo/wechatplugin/config/WeChatPluginImageSchema.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
config
;
public
class
WeChatPluginImageSchema
{
public
static
final
String
SCHEMA_ASSETS
=
"assets://"
;
public
static
final
String
SCHMEA_HTTP
=
"http://"
;
public
static
final
String
SCHMEA_HTTPS
=
"https://"
;
public
static
final
String
SCHMEA_FILE
=
"file://"
;
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/config/WechatPluginConfig.java
浏览文件 @
20a7d2e2
...
@@ -16,4 +16,6 @@ public class WechatPluginConfig {
...
@@ -16,4 +16,6 @@ public class WechatPluginConfig {
public
static
final
String
TEXT
=
"text"
;
public
static
final
String
TEXT
=
"text"
;
public
static
final
String
IMAGE
=
"image"
;
public
static
final
String
THUMBNAIL
=
"thumbnail"
;
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/handler/WeChatPluginHandler.kt
浏览文件 @
20a7d2e2
package
com.jarvanmo.wechatplugin.handler
package
com.jarvanmo.wechatplugin.handler
import
android.annotation.SuppressLint
import
android.graphics.BitmapFactory
import
android.content.Context
import
android.util.Log
import
com.jarvanmo.wechatplugin.WechatPlugin
import
com.jarvanmo.wechatplugin.config.WeChatPluginImageSchema
import
com.jarvanmo.wechatplugin.config.WeChatPluginMethods
import
com.jarvanmo.wechatplugin.config.WeChatPluginMethods
import
com.jarvanmo.wechatplugin.config.WechatPluginConfig
import
com.jarvanmo.wechatplugin.config.WechatPluginConfig
import
com.jarvanmo.wechatplugin.utils.AssetManagerUtil
import
com.jarvanmo.wechatplugin.utils.FileUtil
import
com.jarvanmo.wechatplugin.utils.WeChatThumbnailUtil
import
com.tencent.mm.opensdk.modelbase.BaseResp
import
com.tencent.mm.opensdk.modelbase.BaseResp
import
com.tencent.mm.opensdk.modelmsg.SendMessageToWX
import
com.tencent.mm.opensdk.modelmsg.*
import
com.tencent.mm.opensdk.modelmsg.WXMediaMessage
import
com.tencent.mm.opensdk.modelmsg.WXMiniProgramObject
import
com.tencent.mm.opensdk.modelmsg.WXTextObject
import
com.tencent.mm.opensdk.openapi.IWXAPI
import
com.tencent.mm.opensdk.openapi.IWXAPI
import
io.flutter.plugin.common.MethodCall
import
io.flutter.plugin.common.MethodCall
import
io.flutter.plugin.common.MethodChannel
import
io.flutter.plugin.common.MethodChannel
import
top.zibin.luban.Luban
import
io.flutter.plugin.common.PluginRegistry
import
java.io.File
import
java.net.URL
@SuppressLint
(
"StaticFieldLeak"
)
/***
/***
* Created by mo on 2018/8/8
* Created by mo on 2018/8/8
* 冷风如刀,以大地为砧板,视众生为鱼肉。
* 冷风如刀,以大地为砧板,视众生为鱼肉。
...
@@ -26,7 +29,7 @@ object WeChatPluginHandler {
...
@@ -26,7 +29,7 @@ object WeChatPluginHandler {
private
var
channel
:
MethodChannel
?
=
null
private
var
channel
:
MethodChannel
?
=
null
private
var
context
:
Context
?
=
null
private
var
registrar
:
PluginRegistry
.
Registrar
?
=
null
fun
apiIsNull
()
=
wxApi
==
null
fun
apiIsNull
()
=
wxApi
==
null
...
@@ -39,38 +42,37 @@ object WeChatPluginHandler {
...
@@ -39,38 +42,37 @@ object WeChatPluginHandler {
this
.
wxApi
=
wxApi
this
.
wxApi
=
wxApi
}
}
fun
set
Context
(
context
:
Context
)
{
fun
set
Registrar
(
registrar
:
PluginRegistry
.
Registrar
)
{
this
.
context
=
context
.
applicationContext
this
.
registrar
=
registrar
}
}
fun
handle
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
fun
handle
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
when
(
call
.
method
)
{
when
(
call
.
method
)
{
WeChatPluginMethods
.
SHARE_TEXT
->
shareText
(
call
)
WeChatPluginMethods
.
SHARE_TEXT
->
shareText
(
call
,
result
)
WeChatPluginMethods
.
SHARE_MINI_PROGRAM
->
shareMiniProgram
(
call
)
WeChatPluginMethods
.
SHARE_MINI_PROGRAM
->
shareMiniProgram
(
call
,
result
)
WeChatPluginMethods
.
SHARE_IMAGE
->
shareImage
(
call
,
result
)
else
->
{
else
->
{
result
.
notImplemented
()
result
.
notImplemented
()
}
}
}
}
}
}
private
fun
shareText
(
call
:
MethodCall
)
{
private
fun
shareText
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
val
textObj
=
WXTextObject
()
val
textObj
=
WXTextObject
()
textObj
.
text
=
call
.
argument
(
WechatPluginConfig
.
TEXT
)
textObj
.
text
=
call
.
argument
(
WechatPluginConfig
.
TEXT
)
val
msg
=
WXMediaMessage
()
val
msg
=
WXMediaMessage
()
msg
.
mediaObject
=
textObj
msg
.
mediaObject
=
textObj
msg
.
description
=
call
.
argument
(
WechatPluginConfig
.
TEXT
)
msg
.
description
=
call
.
argument
(
WechatPluginConfig
.
TEXT
)
val
req
=
SendMessageToWX
.
Req
()
val
req
=
SendMessageToWX
.
Req
()
req
.
transaction
=
call
.
argument
(
WechatPluginConfig
.
TRANSACTION
)
req
.
message
=
msg
req
.
message
=
msg
req
.
scene
=
getScene
(
call
.
argument
(
WechatPluginConfig
.
SCENE
)
)
setCommonArguments
(
call
,
req
)
wxApi
?.
sendReq
(
req
)
wxApi
?.
sendReq
(
req
)
result
.
success
(
true
)
}
}
private
fun
shareMiniProgram
(
call
:
MethodCall
)
{
private
fun
shareMiniProgram
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
val
miniProgramObj
=
WXMiniProgramObject
()
val
miniProgramObj
=
WXMiniProgramObject
()
miniProgramObj
.
webpageUrl
=
call
.
argument
(
"webPageUrl"
)
// 兼容低版本的网页链接
miniProgramObj
.
webpageUrl
=
call
.
argument
(
"webPageUrl"
)
// 兼容低版本的网页链接
miniProgramObj
.
miniprogramType
=
call
.
argument
(
"miniProgramType"
)
// 正式版:0,测试版:1,体验版:2
miniProgramObj
.
miniprogramType
=
call
.
argument
(
"miniProgramType"
)
// 正式版:0,测试版:1,体验版:2
...
@@ -79,16 +81,69 @@ object WeChatPluginHandler {
...
@@ -79,16 +81,69 @@ object WeChatPluginHandler {
val
msg
=
WXMediaMessage
(
miniProgramObj
)
val
msg
=
WXMediaMessage
(
miniProgramObj
)
msg
.
title
=
call
.
argument
(
"title"
)
// 小程序消息title
msg
.
title
=
call
.
argument
(
"title"
)
// 小程序消息title
msg
.
description
=
call
.
argument
(
"description"
)
// 小程序消息desc
msg
.
description
=
call
.
argument
(
"description"
)
// 小程序消息desc
// msg.thumbData = getThumb() // 小程序消息封面图片,小于128k
var
thumbnail
:
String
?
=
call
.
argument
(
WechatPluginConfig
.
THUMBNAIL
)
thumbnail
=
thumbnail
?:
""
if
(
thumbnail
.
isNullOrBlank
())
{
msg
.
thumbData
=
null
}
else
{
msg
.
thumbData
=
WeChatThumbnailUtil
.
thumbnailForMiniProgram
(
thumbnail
,
registrar
)
}
val
req
=
SendMessageToWX
.
Req
()
val
req
=
SendMessageToWX
.
Req
()
req
.
transaction
=
call
.
argument
(
"transaction"
)
setCommonArguments
(
call
,
req
)
req
.
message
=
msg
req
.
message
=
msg
req
.
scene
=
call
.
argument
(
"scene"
)
// 目前支持会话
wxApi
?.
sendReq
(
req
)
wxApi
.
sendReq
(
req
)
result
.
success
(
true
)
}
private
fun
shareImage
(
call
:
MethodCall
,
result
:
MethodChannel
.
Result
)
{
val
imagePath
=
call
.
argument
<
String
>(
WechatPluginConfig
.
IMAGE
)
val
imgObj
=
createWxImageObject
(
imagePath
)
if
(
imgObj
==
null
)
{
result
.
error
(
WechatPlugin
.
TAG
,
WechatPlugin
.
FILE_NOT_EXIST
,
imagePath
)
return
}
// val bmp = BitmapFactory.decodeResource(getResources(), R.drawable.send_img)
// val imgObj = WXImageObject(bmp)
//
val
msg
=
WXMediaMessage
()
msg
.
mediaObject
=
imgObj
//
// val thumbBmp = Bitmap.createScaledBitmap(bmp, THUMB_SIZE, THUMB_SIZE, true)
// bmp.recycle()
msg
.
thumbData
=
WeChatThumbnailUtil
.
thumbnailForCommon
(
call
.
argument
(
WechatPluginConfig
.
THUMBNAIL
),
registrar
)
//
val
req
=
SendMessageToWX
.
Req
()
setCommonArguments
(
call
,
req
)
// req.message = msg
wxApi
?.
sendReq
(
req
)
result
.
success
(
true
)
}
}
private
fun
createWxImageObject
(
imagePath
:
String
):
WXImageObject
?{
var
imgObj
:
WXImageObject
?
=
null
var
imageFile
:
File
?
=
null
if
(
imagePath
.
startsWith
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
)){
val
key
=
imagePath
.
substring
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
.
length
,
imagePath
.
length
)
val
assetFileDescriptor
=
AssetManagerUtil
.
openAsset
(
registrar
,
key
,
""
)
imageFile
=
FileUtil
.
createTmpFile
(
assetFileDescriptor
)
}
else
if
(
imagePath
.
startsWith
(
WeChatPluginImageSchema
.
SCHMEA_FILE
)){
imageFile
=
File
(
imagePath
)
}
if
(
imageFile
!=
null
&&
imageFile
.
exists
()){
imgObj
=
WXImageObject
()
imgObj
.
setImagePath
(
imagePath
)
}
else
{
Log
.
d
(
WechatPlugin
.
TAG
,
WechatPlugin
.
FILE_NOT_EXIST
)
}
return
imgObj
}
fun
onResp
(
resp
:
BaseResp
)
{
fun
onResp
(
resp
:
BaseResp
)
{
val
result
=
mapOf
(
val
result
=
mapOf
(
"errStr"
to
resp
.
errStr
,
"errStr"
to
resp
.
errStr
,
...
@@ -104,13 +159,16 @@ object WeChatPluginHandler {
...
@@ -104,13 +159,16 @@ object WeChatPluginHandler {
}
}
private
fun
getScene
(
value
:
String
)
=
when
(
value
)
{
private
fun
getScene
(
value
:
String
)
=
when
(
value
.
toLowerCase
()
)
{
WechatPluginConfig
.
TIMELINE
->
SendMessageToWX
.
Req
.
WXSceneTimeline
WechatPluginConfig
.
TIMELINE
->
SendMessageToWX
.
Req
.
WXSceneTimeline
WechatPluginConfig
.
SESSION
->
SendMessageToWX
.
Req
.
WXSceneSession
WechatPluginConfig
.
SESSION
->
SendMessageToWX
.
Req
.
WXSceneSession
WechatPluginConfig
.
FAVORITE
->
SendMessageToWX
.
Req
.
WXSceneFavorite
WechatPluginConfig
.
FAVORITE
->
SendMessageToWX
.
Req
.
WXSceneFavorite
else
->
SendMessageToWX
.
Req
.
WXSceneTimeline
else
->
SendMessageToWX
.
Req
.
WXSceneTimeline
}
}
private
fun
setCommonArguments
(
call
:
MethodCall
,
req
:
SendMessageToWX
.
Req
)
{
req
.
transaction
=
call
.
argument
(
WechatPluginConfig
.
TRANSACTION
)
req
.
scene
=
getScene
(
call
.
argument
(
WechatPluginConfig
.
SCENE
))
}
}
}
\ No newline at end of file
android/src/main/kotlin/com/jarvanmo/wechatplugin/utils/AssetManagerUtil.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
utils
;
import
android.content.res.AssetFileDescriptor
;
import
android.content.res.AssetManager
;
import
java.io.IOException
;
import
io.flutter.plugin.common.PluginRegistry
;
public
final
class
AssetManagerUtil
{
private
AssetManagerUtil
(){
throw
new
RuntimeException
(
"can't do this"
);
}
public
static
AssetFileDescriptor
openAsset
(
PluginRegistry
.
Registrar
registrar
,
String
assetKey
,
String
assetPackage
)
{
AssetFileDescriptor
fd
=
null
;
AssetManager
assetManager
=
registrar
.
context
().
getAssets
();
String
key
=
registrar
.
lookupKeyForAsset
(
assetKey
,
assetPackage
);
try
{
fd
=
assetManager
.
openFd
(
key
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
return
fd
;
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/utils/CompressImageUtil.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
utils
;
import
android.content.Context
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.ArrayList
;
import
java.util.List
;
import
top.zibin.luban.Luban
;
class
CompressImageUtil
{
private
CompressImageUtil
(){}
public
static
File
compressUtilSmallerThan
(
int
size
,
File
file
,
Context
context
){
File
result
=
null
;
File
tmp
=
file
;
List
<
File
>
dirtyFiles
=
new
ArrayList
<>();
try
{
while
(
tmp
.
length
()
>
size
*
1024
){
List
<
File
>
compressedFiles
=
Luban
.
with
(
context
)
.
ignoreBy
(
size
)
.
load
(
tmp
)
.
setTargetDir
(
context
.
getCacheDir
().
getAbsolutePath
())
.
get
();
tmp
=
compressedFiles
.
get
(
0
);
dirtyFiles
.
add
(
tmp
);
}
result
=
tmp
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
for
(
File
dirtyFile
:
dirtyFiles
)
{
if
(
dirtyFile
!=
result
){
dirtyFile
.
delete
();
}
}
return
result
;
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/utils/FileUtil.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
utils
;
import
android.content.res.AssetFileDescriptor
;
import
java.io.File
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.util.UUID
;
import
okio.BufferedSink
;
import
okio.Okio
;
import
okio.Source
;
public
class
FileUtil
{
public
static
File
createTmpFile
(
AssetFileDescriptor
fileDescriptor
){
if
(
fileDescriptor
==
null
)
{
return
null
;
}
File
file
=
null
;
BufferedSink
sink
=
null
;
Source
source
=
null
;
OutputStream
outputStream
=
null
;
try
{
file
=
File
.
createTempFile
(
UUID
.
randomUUID
().
toString
(),
".png"
);
outputStream
=
new
FileOutputStream
(
file
);
sink
=
Okio
.
buffer
(
Okio
.
sink
(
outputStream
));
source
=
Okio
.
source
(
fileDescriptor
.
createInputStream
());
sink
.
writeAll
(
source
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
finally
{
if
(
sink
!=
null
){
try
{
sink
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
source
!=
null
)
{
try
{
source
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
outputStream
!=
null
)
{
try
{
outputStream
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
return
file
;
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/utils/Util.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
utils
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.RandomAccessFile
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.net.URLConnection
;
import
java.security.MessageDigest
;
import
junit.framework.Assert
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
android.graphics.Bitmap.CompressFormat
;
import
android.util.Log
;
public
class
Util
{
private
static
final
String
TAG
=
"SDK_Sample.Util"
;
public
static
byte
[]
bmpToByteArray
(
final
Bitmap
bmp
,
final
boolean
needRecycle
)
{
ByteArrayOutputStream
output
=
new
ByteArrayOutputStream
();
bmp
.
compress
(
CompressFormat
.
PNG
,
100
,
output
);
if
(
needRecycle
)
{
bmp
.
recycle
();
}
byte
[]
result
=
output
.
toByteArray
();
try
{
output
.
close
();
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
}
return
result
;
}
public
static
byte
[]
getHtmlByteArray
(
final
String
url
)
{
URL
htmlUrl
=
null
;
InputStream
inStream
=
null
;
try
{
htmlUrl
=
new
URL
(
url
);
URLConnection
connection
=
htmlUrl
.
openConnection
();
HttpURLConnection
httpConnection
=
(
HttpURLConnection
)
connection
;
int
responseCode
=
httpConnection
.
getResponseCode
();
if
(
responseCode
==
HttpURLConnection
.
HTTP_OK
){
inStream
=
httpConnection
.
getInputStream
();
}
}
catch
(
MalformedURLException
e
)
{
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
byte
[]
data
=
inputStreamToByte
(
inStream
);
return
data
;
}
public
static
byte
[]
inputStreamToByte
(
InputStream
is
)
{
try
{
ByteArrayOutputStream
bytestream
=
new
ByteArrayOutputStream
();
int
ch
;
while
((
ch
=
is
.
read
())
!=
-
1
)
{
bytestream
.
write
(
ch
);
}
byte
imgdata
[]
=
bytestream
.
toByteArray
();
bytestream
.
close
();
return
imgdata
;
}
catch
(
Exception
e
){
e
.
printStackTrace
();
}
return
null
;
}
public
static
byte
[]
readFromFile
(
String
fileName
,
int
offset
,
int
len
)
{
if
(
fileName
==
null
)
{
return
null
;
}
File
file
=
new
File
(
fileName
);
if
(!
file
.
exists
())
{
Log
.
i
(
TAG
,
"readFromFile: file not found"
);
return
null
;
}
if
(
len
==
-
1
)
{
len
=
(
int
)
file
.
length
();
}
Log
.
d
(
TAG
,
"readFromFile : offset = "
+
offset
+
" len = "
+
len
+
" offset + len = "
+
(
offset
+
len
));
if
(
offset
<
0
){
Log
.
e
(
TAG
,
"readFromFile invalid offset:"
+
offset
);
return
null
;
}
if
(
len
<=
0
){
Log
.
e
(
TAG
,
"readFromFile invalid len:"
+
len
);
return
null
;
}
if
(
offset
+
len
>
(
int
)
file
.
length
()){
Log
.
e
(
TAG
,
"readFromFile invalid file len:"
+
file
.
length
());
return
null
;
}
byte
[]
b
=
null
;
try
{
RandomAccessFile
in
=
new
RandomAccessFile
(
fileName
,
"r"
);
b
=
new
byte
[
len
];
// ���������ļ���С������
in
.
seek
(
offset
);
in
.
readFully
(
b
);
in
.
close
();
}
catch
(
Exception
e
)
{
Log
.
e
(
TAG
,
"readFromFile : errMsg = "
+
e
.
getMessage
());
e
.
printStackTrace
();
}
return
b
;
}
private
static
final
int
MAX_DECODE_PICTURE_SIZE
=
1920
*
1440
;
public
static
Bitmap
extractThumbNail
(
final
String
path
,
final
int
height
,
final
int
width
,
final
boolean
crop
)
{
Assert
.
assertTrue
(
path
!=
null
&&
!
path
.
equals
(
""
)
&&
height
>
0
&&
width
>
0
);
BitmapFactory
.
Options
options
=
new
BitmapFactory
.
Options
();
try
{
options
.
inJustDecodeBounds
=
true
;
Bitmap
tmp
=
BitmapFactory
.
decodeFile
(
path
,
options
);
if
(
tmp
!=
null
)
{
tmp
.
recycle
();
tmp
=
null
;
}
Log
.
d
(
TAG
,
"extractThumbNail: round="
+
width
+
"x"
+
height
+
", crop="
+
crop
);
final
double
beY
=
options
.
outHeight
*
1.0
/
height
;
final
double
beX
=
options
.
outWidth
*
1.0
/
width
;
Log
.
d
(
TAG
,
"extractThumbNail: extract beX = "
+
beX
+
", beY = "
+
beY
);
options
.
inSampleSize
=
(
int
)
(
crop
?
(
beY
>
beX
?
beX
:
beY
)
:
(
beY
<
beX
?
beX
:
beY
));
if
(
options
.
inSampleSize
<=
1
)
{
options
.
inSampleSize
=
1
;
}
// NOTE: out of memory error
while
(
options
.
outHeight
*
options
.
outWidth
/
options
.
inSampleSize
>
MAX_DECODE_PICTURE_SIZE
)
{
options
.
inSampleSize
++;
}
int
newHeight
=
height
;
int
newWidth
=
width
;
if
(
crop
)
{
if
(
beY
>
beX
)
{
newHeight
=
(
int
)
(
newWidth
*
1.0
*
options
.
outHeight
/
options
.
outWidth
);
}
else
{
newWidth
=
(
int
)
(
newHeight
*
1.0
*
options
.
outWidth
/
options
.
outHeight
);
}
}
else
{
if
(
beY
<
beX
)
{
newHeight
=
(
int
)
(
newWidth
*
1.0
*
options
.
outHeight
/
options
.
outWidth
);
}
else
{
newWidth
=
(
int
)
(
newHeight
*
1.0
*
options
.
outWidth
/
options
.
outHeight
);
}
}
options
.
inJustDecodeBounds
=
false
;
Log
.
i
(
TAG
,
"bitmap required size="
+
newWidth
+
"x"
+
newHeight
+
", orig="
+
options
.
outWidth
+
"x"
+
options
.
outHeight
+
", sample="
+
options
.
inSampleSize
);
Bitmap
bm
=
BitmapFactory
.
decodeFile
(
path
,
options
);
if
(
bm
==
null
)
{
Log
.
e
(
TAG
,
"bitmap decode failed"
);
return
null
;
}
Log
.
i
(
TAG
,
"bitmap decoded size="
+
bm
.
getWidth
()
+
"x"
+
bm
.
getHeight
());
final
Bitmap
scale
=
Bitmap
.
createScaledBitmap
(
bm
,
newWidth
,
newHeight
,
true
);
if
(
scale
!=
null
)
{
bm
.
recycle
();
bm
=
scale
;
}
if
(
crop
)
{
final
Bitmap
cropped
=
Bitmap
.
createBitmap
(
bm
,
(
bm
.
getWidth
()
-
width
)
>>
1
,
(
bm
.
getHeight
()
-
height
)
>>
1
,
width
,
height
);
if
(
cropped
==
null
)
{
return
bm
;
}
bm
.
recycle
();
bm
=
cropped
;
Log
.
i
(
TAG
,
"bitmap croped size="
+
bm
.
getWidth
()
+
"x"
+
bm
.
getHeight
());
}
return
bm
;
}
catch
(
final
OutOfMemoryError
e
)
{
Log
.
e
(
TAG
,
"decode bitmap failed: "
+
e
.
getMessage
());
options
=
null
;
}
return
null
;
}
}
android/src/main/kotlin/com/jarvanmo/wechatplugin/utils/WeChatThumbnailUtil.java
0 → 100644
浏览文件 @
20a7d2e2
package
com
.
jarvanmo
.
wechatplugin
.
utils
;
import
android.content.res.AssetFileDescriptor
;
import
android.graphics.Bitmap
;
import
android.graphics.BitmapFactory
;
import
com.jarvanmo.wechatplugin.config.WeChatPluginImageSchema
;
import
java.io.File
;
import
java.io.IOException
;
import
io.flutter.plugin.common.PluginRegistry
;
import
okio.Okio
;
import
okio.Source
;
public
class
WeChatThumbnailUtil
{
private
static
final
int
COMMON_THUMB_SIZE
=
150
;
private
WeChatThumbnailUtil
()
{
}
public
static
byte
[]
thumbnailForMiniProgram
(
String
thumbnail
,
PluginRegistry
.
Registrar
registrar
)
{
byte
[]
result
=
null
;
if
(
thumbnail
.
startsWith
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
))
{
result
=
fromAssetForMiniProgram
(
thumbnail
,
registrar
);
}
return
result
;
}
private
static
byte
[]
fromAssetForMiniProgram
(
String
thumbnail
,
PluginRegistry
.
Registrar
registrar
){
byte
[]
result
=
null
;
String
key
=
thumbnail
.
substring
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
.
length
(),
thumbnail
.
length
());
AssetFileDescriptor
fileDescriptor
=
AssetManagerUtil
.
openAsset
(
registrar
,
key
,
""
);
if
(
fileDescriptor
!=
null
&&
fileDescriptor
.
getLength
()
<=
128
*
1024
)
{
try
{
Source
source
=
Okio
.
source
(
fileDescriptor
.
createInputStream
());
result
=
Okio
.
buffer
(
source
).
readByteArray
();
source
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
else
if
(
fileDescriptor
!=
null
&&
fileDescriptor
.
getLength
()
>
128
*
1024
)
{
File
file
=
FileUtil
.
createTmpFile
(
fileDescriptor
);
if
(
file
==
null
)
{
return
null
;
}
File
snapshot
=
CompressImageUtil
.
compressUtilSmallerThan
(
128
,
file
,
registrar
.
context
());
if
(
snapshot
==
null
)
{
return
null
;
}
try
{
result
=
Okio
.
buffer
(
Okio
.
source
(
snapshot
)).
readByteArray
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
return
result
;
}
public
static
byte
[]
thumbnailForCommon
(
String
thumbnail
,
PluginRegistry
.
Registrar
registrar
){
byte
[]
result
=
null
;
if
(
thumbnail
.
startsWith
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
))
{
result
=
fromAssetForCommon
(
thumbnail
,
registrar
);
}
return
result
;
}
private
static
byte
[]
fromAssetForCommon
(
String
thumbnail
,
PluginRegistry
.
Registrar
registrar
){
int
size
=
32
;
byte
[]
result
=
null
;
String
key
=
thumbnail
.
substring
(
WeChatPluginImageSchema
.
SCHEMA_ASSETS
.
length
(),
thumbnail
.
length
());
AssetFileDescriptor
fileDescriptor
=
AssetManagerUtil
.
openAsset
(
registrar
,
key
,
""
);
if
(
fileDescriptor
!=
null
&&
fileDescriptor
.
getLength
()
<=
size
*
1024
)
{
try
{
Source
source
=
Okio
.
source
(
fileDescriptor
.
createInputStream
());
result
=
Okio
.
buffer
(
source
).
readByteArray
();
source
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
else
if
(
fileDescriptor
!=
null
&&
fileDescriptor
.
getLength
()
>
size
*
1024
)
{
File
file
=
FileUtil
.
createTmpFile
(
fileDescriptor
);
if
(
file
==
null
)
{
return
null
;
}
File
snapshot
=
CompressImageUtil
.
compressUtilSmallerThan
(
size
,
file
,
registrar
.
context
());
if
(
snapshot
==
null
)
{
return
null
;
}
try
{
result
=
Okio
.
buffer
(
Okio
.
source
(
snapshot
)).
readByteArray
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
// Bitmap bmp = BitmapFactory.decodeStream(new URL(url).openStream());
// Bitmap thumbBmp = Bitmap.createScaledBitmap(bmp, COMMON_THUMB_SIZE, COMMON_THUMB_SIZE, true);
// bmp.recycle();
// result = Util.bmpToByteArray(thumbBmp, true);
return
result
;
}
}
example/android/build.gradle
浏览文件 @
20a7d2e2
buildscript
{
buildscript
{
ext
.
kotlin_version
=
'1.
1.51
'
ext
.
kotlin_version
=
'1.
2.60
'
repositories
{
repositories
{
google
()
google
()
jcenter
()
jcenter
()
...
...
example/lib/main.dart
浏览文件 @
20a7d2e2
...
@@ -18,6 +18,7 @@ class _MyAppState extends State<MyApp> {
...
@@ -18,6 +18,7 @@ class _MyAppState extends State<MyApp> {
void
initState
()
{
void
initState
()
{
super
.
initState
();
super
.
initState
();
initPlatformState
();
initPlatformState
();
}
}
// Platform messages are asynchronous, so we initialize in an async method.
// Platform messages are asynchronous, so we initialize in an async method.
...
...
lib/src/wechat_share_models.dart
浏览文件 @
20a7d2e2
import
'package:wechat_plugin/src/wechat_scene.dart'
;
import
'package:wechat_plugin/src/wechat_scene.dart'
;
const
String
_scene
=
"scene"
;
const
String
_transaction
=
"transaction"
;
const
String
_thumbnail
=
"thumbnail"
;
class
WeChatShareTextModel
{
class
WeChatShareTextModel
{
final
String
text
;
final
String
text
;
final
String
transaction
;
final
String
transaction
;
...
@@ -11,17 +15,11 @@ class WeChatShareTextModel {
...
@@ -11,17 +15,11 @@ class WeChatShareTextModel {
this
.
scene
=
scene
??
WeChatScene
.
TIMELINE
;
this
.
scene
=
scene
??
WeChatScene
.
TIMELINE
;
Map
toMap
()
{
Map
toMap
()
{
return
{
return
{
"text"
:
text
,
_transaction:
transaction
,
_scene:
scene
.
toString
()};
"text"
:
text
,
"transaction"
:
transaction
,
"scene"
:
scene
.
toString
()
};
}
}
}
}
class
WeChatShareMiniProgramModel
{
class
WeChatMiniProgramModel
{
static
const
int
MINI_PROGRAM_TYPE_RELEASE
=
0
;
static
const
int
MINI_PROGRAM_TYPE_RELEASE
=
0
;
static
const
int
MINI_PROGRAM_TYPE_TEST
=
1
;
static
const
int
MINI_PROGRAM_TYPE_TEST
=
1
;
static
const
int
MINI_PROGRAM_TYPE_PREVIEW
=
2
;
static
const
int
MINI_PROGRAM_TYPE_PREVIEW
=
2
;
...
@@ -41,32 +39,56 @@ class WeChatMiniProgramModel {
...
@@ -41,32 +39,56 @@ class WeChatMiniProgramModel {
final
String
thumbnail
;
final
String
thumbnail
;
WeChatMiniProgramModel
(
WeChat
Share
MiniProgramModel
(
{
this
.
webPageUrl
,
{
this
.
webPageUrl
,
int
miniProgramType
,
int
miniProgramType
,
this
.
userName
,
this
.
userName
,
this
.
path
,
this
.
path
,
this
.
title
,
this
.
title
,
this
.
description
,
this
.
description
,
this
.
thumbnail
,
this
.
thumbnail
,
String
transaction
})
String
transaction
})
:
this
.
transaction
=
transaction
??
"miniProgram"
,
:
this
.
transaction
=
transaction
??
"miniProgram"
,
this
.
miniProgramType
=
miniProgramType
??
MINI_PROGRAM_TYPE_RELEASE
,
this
.
miniProgramType
=
miniProgramType
??
MINI_PROGRAM_TYPE_RELEASE
,
assert
(
webPageUrl
!=
null
&&
webPageUrl
.
isNotEmpty
),
assert
(
webPageUrl
!=
null
&&
webPageUrl
.
isNotEmpty
),
assert
(
userName
!=
null
&&
userName
.
isNotEmpty
)
,
assert
(
userName
!=
null
&&
userName
.
isNotEmpty
)
,
assert
(
path
!=
null
&&
path
.
isNotEmpty
);
assert
(
path
!=
null
&&
path
.
isNotEmpty
);
Map
toMap
(){
Map
toMap
()
{
return
{
'webPageUrl'
:
webPageUrl
,
"miniProgramType"
:
miniProgramType
,
"userName"
:
userName
,
"path"
:
path
,
"title"
:
title
,
"description"
:
description
,
_transaction:
transaction
,
_scene:
scene
,
_thumbnail:
thumbnail
};
}
}
class
WeChatShareImageModel
{
final
String
transaction
;
final
WeChatScene
scene
;
final
String
image
;
final
String
thumbnail
;
WeChatShareImageModel
(
{
String
transaction
,
WeChatScene
scene
,
this
.
image
,
String
thumbnail
})
:
this
.
transaction
=
transaction
??
"text"
,
this
.
scene
=
scene
??
WeChatScene
.
TIMELINE
,
this
.
thumbnail
=
thumbnail
??
""
,
assert
(
image
!=
null
),
assert
(
thumbnail
!=
null
);
Map
toMap
()
{
return
{
return
{
'webPageUrl'
:
webPageUrl
,
_transaction:
transaction
,
"miniProgramType"
:
miniProgramType
,
_scene:
scene
.
toString
(),
"userName"
:
userName
,
"image"
:
image
,
"path"
:
path
,
_thumbnail:
thumbnail
"title"
:
title
,
"description"
:
description
,
"transaction"
:
transaction
,
"scene"
:
scene
,
"thumbnail"
:
thumbnail
};
};
}
}
}
}
lib/wechat_plugin.dart
浏览文件 @
20a7d2e2
...
@@ -31,7 +31,7 @@ class WechatPlugin {
...
@@ -31,7 +31,7 @@ class WechatPlugin {
return
await
_channel
.
invokeMethod
(
"shareText"
,
model
.
toMap
());
return
await
_channel
.
invokeMethod
(
"shareText"
,
model
.
toMap
());
}
}
static
Future
shareMiniProgram
(
WeChatMiniProgramModel
model
)
async
{
static
Future
shareMiniProgram
(
WeChat
Share
MiniProgramModel
model
)
async
{
return
await
_channel
.
invokeMethod
(
"shareMiniProgram"
,
model
.
toMap
());
return
await
_channel
.
invokeMethod
(
"shareMiniProgram"
,
model
.
toMap
());
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论