文档模板:v0.4.2
react-native-create-thumbnail
本项目基于 react-native-create-thumbnail 开发。
该第三方库的仓库已迁移至 GitCode,且支持直接从 npm 下载,新的包名为:@react-native-ohos/react-native-create-thumbnail,版本所属关系如下:
| 三方库名称 | 三方库版本(npm地址) | 发布信息 | 支持RN版本 | Autolink | 编译API版本 | 社区基线版本 | 源码地址 |
|---|---|---|---|---|---|---|---|
| @react-native-ohos/react-native-create-thumbnail | ~2.3.0(开发中) | GitCode Releases | 0.84.* / 0.86.* | 是 | API12+ | 2.2.0 | master |
| @react-native-ohos/react-native-create-thumbnail | ~2.2.0 | GitCode Releases | 0.82.* | 是 | API12+ | 2.0.1 | br_rnoh0.82 |
| @react-native-ohos/react-native-create-thumbnail | 2.1.0 | GitCode Releases | 0.77.* | 否 | API12+ | 2.0.1 | br_rnoh0.77 |
| @react-native-ohos/react-native-create-thumbnail | ~2.0.1 | GitCode Releases | 0.72.* | 是 | API12+ | 2.0.0 | br_rnoh0.72 |
| @react-native-oh-tpl/react-native-create-thumbnail | <= 2.0.0-0.0.4@deprecated | GitHub Releases(deprecated) | 0.72.* | 否 | API12+ | 2.0.0 | sig |
简介
React Native 视频缩略图生成库。
基于 HarmonyOS 媒体能力实现视频缩略图生成,支持本地和远程视频文件,带存储/缓存管理。
下载安装
进入到工程目录并输入以下命令:
npm
npm install @react-native-ohos/react-native-create-thumbnail
yarn
yarn add @react-native-ohos/react-native-create-thumbnail
Link
| 版本 | 是否支持autolink | RN框架版本 |
|---|---|---|
| ~2.3.0 | 是 | 0.84.* / 0.86.* |
使用AutoLink的工程需要根据该文档配置,Autolink框架指导文档:https://gitcode.com/CPF-RN/ohos_react_native/blob/main/docs/zh-cn/02-开发/02-开发指南/Autolinking.md
如您使用的版本支持 Autolink,并且工程已接入 Autolink,可跳过ManualLink配置。
ManualLink: 此步骤为手动配置原生依赖项的指导
首先需要使用 DevEco Studio 打开项目里的 HarmonyOS 工程 harmony。
1. Overrides RN SDK
为了让工程依赖同一个版本的 RN SDK,需要在工程根目录的 oh-package.json5 添加 overrides 字段,指向工程需要使用的 RN SDK 版本。替换的版本既可以是一个具体的版本号,也可以是一个模糊版本,还可以是本地存在的 HAR 包或源码目录。
关于该字段的作用请阅读官方说明
{
"overrides": {
"@rnoh/react-native-openharmony": "~0.86.1" // ohpm 在线版本
// "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // 指向本地 har 包的路径
// "@rnoh/react-native-openharmony" : "./react_native_openharmony" // 指向源码路径
}
}
2. 引入原生端代码
目前有两种方法:
- 通过 har 包引入;
- 直接链接源码。
方法一:通过 har 包引入(推荐)
har 包位于三方库安装路径的 `harmony` 文件夹下。
打开 entry/oh-package.json5,添加以下依赖
"dependencies": {
"@react-native-ohos/react-native-create-thumbnail": "file:../../node_modules/@react-native-ohos/react-native-create-thumbnail/harmony/createThumbnail.har"
}
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
方法二:直接链接源码
如需使用直接链接源码,请参考[直接链接源码说明](https://gitcode.com/CPF-RN/usage-docs/blob/master/zh-cn/link-source-code.md)
3. 配置 CMakeLists 和引入 CreateThumbnailPackage
打开 entry/src/main/cpp/CMakeLists.txt,添加:
project(rnapp)
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_SKIP_BUILD_RPATH TRUE)
set(RNOH_APP_DIR "${CMAKE_CURRENT_SOURCE_DIR}")
set(NODE_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../node_modules")
+ set(OH_MODULES "${CMAKE_CURRENT_SOURCE_DIR}/../../../oh_modules")
set(RNOH_CPP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../../../../react-native-harmony/harmony/cpp")
set(LOG_VERBOSITY_LEVEL 1)
set(CMAKE_ASM_FLAGS "-Wno-error=unused-command-line-argument -Qunused-arguments")
set(CMAKE_CXX_FLAGS "-fstack-protector-strong -Wl,-z,relro,-z,now,-z,noexecstack -s -fPIE -pie")
set(WITH_HITRACE_SYSTRACE 1) # for other CMakeLists.txt files to use
add_compile_definitions(WITH_HITRACE_SYSTRACE)
add_subdirectory("${RNOH_CPP_DIR}" ./rn)
# RNOH_BEGIN: manual_package_linking_1
add_subdirectory("../../../../sample_package/src/main/cpp" ./sample-package)
+ add_subdirectory("${OH_MODULES}/@react-native-ohos/react-native-create-thumbnail/src/main/cpp" ./create-thumbnail)
# RNOH_END: manual_package_linking_1
file(GLOB GENERATED_CPP_FILES "./generated/*.cpp")
add_library(rnoh_app SHARED
${GENERATED_CPP_FILES}
"./PackageProvider.cpp"
"${RNOH_CPP_DIR}/RNOHAppNapiBridge.cpp"
)
target_link_libraries(rnoh_app PUBLIC rnoh)
# RNOH_BEGIN: manual_package_linking_2
target_link_libraries(rnoh_app PUBLIC rnoh_sample_package)
+ target_link_libraries(rnoh_app PUBLIC rnoh_create_thumbnail)
# RNOH_END: manual_package_linking_2
打开 entry/src/main/cpp/PackageProvider.cpp,添加:
#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "CreateThumbnailPackage.h"
using namespace rnoh;
std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
return {
std::make_shared<SamplePackage>(ctx),
+ std::make_shared<CreateThumbnailPackage>(ctx)
};
}
4. 在 ArkTs 侧引入 CreateThumbnailPackage
打开 entry/src/main/ets/RNPackagesFactory.ts,添加:
...
+ import { CreateThumbnailPackage } from '@react-native-ohos/react-native-create-thumbnail/ts';
export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
return [
+ new CreateThumbnailPackage(ctx)
];
}
运行
点击右上角的 sync 按钮
或者在命令行终端执行:
cd entry
ohpm install
然后编译、运行即可。
约束与限制
兼容性
本文档内容基于以下版本验证通过:
- RNOH: 0.84.1; SDK: HarmonyOS 6.0.0 Release SDK; IDE: DevEco Studio 6.0.0.858; ROM: 6.0.0.112 SP12;
- RNOH: 0.86.1; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1 Release; ROM:6.0.0.120 SP7;
权限要求
-
如果使用网络 URL 生成缩略图,应用需要申请网络权限
在
entry/src/main/module.json5中添加
requestPermissions: [
{
name: "ohos.permission.INTERNET",
},
],
使用示例
下面的代码展示了这个库的基本使用场景:
使用时 import 的库名不变。
import React, {useState } from 'react';
import { StyleSheet, Text, Button, ScrollView, View } from 'react-native';
import { createThumbnail } from 'react-native-create-thumbnail';
export default function CreateThumbnailDemo() {
const [text, setText] = useState('');
const CreateThumbnail = async()=> {
try {
let obj = await createThumbnail({
url:'https://media.w3.org/2010/05/sintel/trailer.mp4',
timeStamp: 10000
});
setText(JSON.stringify(obj))
} catch (error) {
console.error('Create thumbnail failed:', error);
setText('Error: ' + error.message);
}
}
return (
<View style={styles.container}>
<View style={styles.titleArea}>
<Text style={styles.title}>Create Thumbnail Demo</Text>
</View>
<View style={styles.inputArea}>
<Text style={styles.baseText}>{text}</Text>
</View>
<ScrollView style={styles.scrollView}>
<View style={{ flexDirection: 'column' }}>
<View style={styles.baseArea}>
<Text style={{flex:1}}>createThumbnail</Text>
<Button title='运行' color='#841584' onPress={CreateThumbnail}></Button>
</View>
</View>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%',
flexDirection: 'column',
alignItems: 'center',
backgroundColor: '#F1F3F5',
},
baseText: {
fontWeight: 'bold',
textAlign:'center',
fontSize:16
},
titleArea:{
width:'90%',
height:'8%',
alignItems:'center',
flexDirection:'row',
},
title: {
width:'90%',
color:'#000000',
textAlign:'left',
fontSize: 30,
},
scrollView: {
width:'90%',
marginHorizontal: 20,
},
inputArea: {
width:'90%',
height:'30%',
borderWidth:2,
borderColor:'#000000',
marginTop:8,
justifyContent:'center',
alignItems:'center',
},
baseArea: {
width:'100%',
height:60,
borderRadius:4,
borderColor:'#000000',
marginTop:8,
backgroundColor:'#FFFFFF',
flexDirection: 'row',
alignItems:'center',
paddingLeft:8,
paddingRight:8
}
});
使用说明
生成缩略图
import { createThumbnail } from 'react-native-create-thumbnail';
// 从远程视频生成缩略图
try {
const thumbnail = await createThumbnail({
url: 'https://example.com/video.mp4',
timeStamp: 10000, // 第10秒
format: 'jpeg',
dirSize: 100, // 缓存目录最大100MB
});
console.log(thumbnail.path); // 缩略图路径
console.log(thumbnail.width); // 缩略图宽度
console.log(thumbnail.height); // 缩略图高度
} catch (error) {
console.error('Create thumbnail failed:', error);
}
使用缓存避免重复生成
try {
const thumbnail = await createThumbnail({
url: 'https://example.com/video.mp4',
timeStamp: 5000,
cacheName: 'my-thumbnail-cache', // 指定缓存名称
});
} catch (error) {
console.error('Create thumbnail failed:', error);
}
添加请求头
try {
const thumbnail = await createThumbnail({
url: 'https://example.com/video.mp4',
timeStamp: 10000,
headers: {
Authorization: 'Bearer token123'
}
});
} catch (error) {
console.error('Create thumbnail failed:', error);
}
接口说明
"Platform"列表示该属性在原三方库上支持的平台。
"OpenHarmony Support"列为 yes 表示 OpenHarmony平台支持该属性;no 则表示不支持;partially 表示部分支持。使用方法跨平台一致,效果对标 iOS 或 Android 的效果。
API
| 名称 | 类型 | 参数类型 | 返回值 | 必填 | 平台 | OpenHarmony平台支持 | 描述 |
|---|---|---|---|---|---|---|---|
| createThumbnail | function | Config | Promise | No | iOS、Android | yes | 生成视频缩略图 |
参数
| 名称 | 类型 | 必填 | 默认值 | 平台 | OpenHarmony平台支持 | 描述 |
|---|---|---|---|---|---|---|
| url | string | yes | — | iOS、Android | yes | 视频文件路径(本地或远程) |
| timeStamp | number | no | 0 | iOS、Android | yes | 缩略图时间戳(毫秒) |
| format | "jpeg" | "png" | no | "jpeg" | iOS、Android | yes | 缩略图格式 |
| dirSize | number | no | 100 | iOS、Android | yes | 缓存目录最大大小(MB) |
| headers | object | no | — | iOS、Android | yes | 加载视频的请求头 |
| cacheName | string | no | — | iOS、Android | yes | 缩略图缓存名称 |
| maxWidth | number | no | 512 | iOS、Android | yes | 最大缩略图宽度(像素) |
| maxHeight | number | no | 512 | iOS、Android | yes | 最大缩略图高度(像素) |
| timeToleranceMs | number | no | — | iOS | no | 时间容差(毫秒) |
| onlySyncedFrames | boolean | no | true | Android | yes | 是否只使用同步帧 |
返回值
| 名称 | 类型 | 必填 | 平台 | OpenHarmony平台支持 | 描述 |
|---|---|---|---|---|---|
| path | string | no | iOS、Android | yes | 生成的缩略图路径 |
| size | number | no | iOS、Android | yes | 缩略图大小(字节) |
| mime | string | no | iOS、Android | yes | 缩略图 MIME 类型 |
| width | number | no | iOS、Android | yes | 缩略图宽度 |
| height | number | no | iOS、Android | yes | 缩略图高度 |
遗留问题
- timeToleranceMs 属性不支持,因为在 HarmonyOS 中没有提供对应的属性 issues#1
其他
无
目录结构
/rntpc_react-native-create-thumbnail # 项目根目录
├── harmony # 鸿蒙适配代码
│ ├── createThumbnail.har # har 包
│ └── createThumbnail # 鸿蒙适配核心代码
│ ├── Index.ets # 鸿蒙适配代码入口
│ ├── ts.ets # TurboModule ArkTS 声明入口
│ └── src/main
│ ├── cpp # C++ 适配代码
│ ├── ets # ArkTS 适配代码(含 codegen 生成代码)
│ └── module.json5 # 模块配置
├── src # RN 代码
│ └── NativeCreateThumbnail.ts # TurboModule Spec 定义
├── example # 示例工程
├── index.js # 入口文件
├── index.d.ts # 类型定义
├── README.md # 中文安装使用方法
├── README_en.md # 英文安装使用方法
└── package.json # 包配置
贡献代码
使用过程中发现任何问题都可以提交 Issue,当然,也非常欢迎提交 PR 。
开源协议
本项目基于 The MIT License (MIT) ,请自由地享受和参与开源。