Skip to content

Commit

Permalink
Merge pull request #1 from react-native-oh-library/dev
Browse files Browse the repository at this point in the history
增加鸿蒙实现
  • Loading branch information
fzxlzy authored Nov 24, 2023
2 parents 5a79ea8 + 8afc46a commit ea35655
Show file tree
Hide file tree
Showing 19 changed files with 372 additions and 3 deletions.
Binary file added harmony/image_picker.har
Binary file not shown.
16 changes: 16 additions & 0 deletions harmony/image_picker/build-profile.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"apiType": "stageMode",
// "buildOption": {
// "externalNativeOptions": {
// "path": "./src/main/cpp/CMakeLists.txt",
// "arguments": "",
// "cppFlags": ""
// },
// },
"targets": [
{
"name": "default",
"runtimeOS": "HarmonyOS"
}
]
}
2 changes: 2 additions & 0 deletions harmony/image_picker/hvigorfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Script for compiling build behavior. It is built in the build plug-in and cannot be modified currently.
export { harTasks } from '@ohos/hvigor-ohos-plugin';
12 changes: 12 additions & 0 deletions harmony/image_picker/oh-package.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"license": "MIT",
"devDependencies": {
"rnoh": "file:../rnoh"
},
"author": "",
"name": "imagePicker",
"description": "Please describe the basic information.",
"main": "ts.ts",
"version": "1.0.0",
"dependencies": {}
}
8 changes: 8 additions & 0 deletions harmony/image_picker/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# the minimum version of CMake.
cmake_minimum_required(VERSION 3.13)
set(CMAKE_VERBOSE_MAKEFILE on)

file(GLOB rnoh_image_picker_SRC CONFIGURE_DEPENDS *.cpp)
add_library(rnoh_image_picker SHARED ${rnoh_image_picker_SRC})
target_include_directories(rnoh_image_picker PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(rnoh_image_picker PUBLIC rnoh)
29 changes: 29 additions & 0 deletions harmony/image_picker/src/main/cpp/RNImagePickerPackage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "RNOH/Package.h"
#include "RNImagePickerTurboModule.h"

using namespace rnoh;
using namespace facebook;
class RNImagePickerFactoryDelegate : public TurboModuleFactoryDelegate
{
public:
SharedTurboModule createTurboModule(Context ctx, const std::string &name) const override
{
if (name == "ImagePicker")
{
return std::make_shared<RNImagePickerTurboModule>(ctx, name);
}
return nullptr;
};
};
namespace rnoh
{
class RNImagePickerPackage: public Package
{
public:
RNImagePickerPackage(Package::Context ctx) : Package(ctx) {}
std::unique_ptr<TurboModuleFactoryDelegate> createTurboModuleFactoryDelegate() override
{
return std::make_unique<RNImagePickerFactoryDelegate>();
}
};
} // namespace rnoh
26 changes: 26 additions & 0 deletions harmony/image_picker/src/main/cpp/RNImagePickerTurboModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
* @generated by codegen project: GenerateModuleH.js
*/
#include "RNImagePickerTurbomodule.h"
using namespace rnoh;
using namespace facebook;

static jsi::Value __hostFunction_RNImagePickerTurboModule_launchCamera(jsi::Runtime &rt,
react::TurboModule &turboModule,
const jsi::Value *args, size_t count) {
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "launchCamera", args, count);
}
static jsi::Value __hostFunction_RNImagePickerTurboModule_launchImageLibrary(jsi::Runtime &rt,
react::TurboModule &turboModule,
const jsi::Value *args, size_t count) {
return static_cast<ArkTSTurboModule &>(turboModule).call(rt, "launchImageLibrary", args, count);
}

RNImagePickerTurboModule::RNImagePickerTurboModule(const ArkTSTurboModule::Context ctx, const std::string name)
: ArkTSTurboModule(ctx, name) {
methodMap_["launchCamera"] = MethodMetadata{0, __hostFunction_RNImagePickerTurboModule_launchCamera};
methodMap_["launchImageLibrary"] = MethodMetadata{0, __hostFunction_RNImagePickerTurboModule_launchImageLibrary};
}
21 changes: 21 additions & 0 deletions harmony/image_picker/src/main/cpp/RNImagePickerTurbomodule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen).
*
* Do not edit this file as changes may cause incorrect behavior and will be lost
* once the code is regenerated.
*
* @generated by codegen project: GenerateModuleH.js
*/
#pragma once

#include <ReactCommon/TurboModule.h>
#include "RNOH/ArkTSTurboModule.h"

namespace rnoh{

class JSI_EXPORT RNImagePickerTurboModule : public ArkTSTurboModule{
public:
RNImagePickerTurboModule(const ArkTSTurboModule::Context ctx, const std::string name);
};

} // namespace rnoh
24 changes: 24 additions & 0 deletions harmony/image_picker/src/main/ets/ImagePickerPackage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { RNPackage, TurboModulesFactory } from 'rnoh/ts';
import type { TurboModule, TurboModuleContext } from 'rnoh/ts';
import { ImagePickerTurboModule } from './ImagePickerTurboModule';

class ImagePickerTurboModulesFactory extends TurboModulesFactory {

createTurboModule(name: string): TurboModule | null {
if (name === 'ImagePicker') {
return new ImagePickerTurboModule(this.ctx);
}
return null;
}

hasTurboModule(name: string): boolean {
return name === 'ImagePicker';
}

}

export class ImagePickerViewPackage extends RNPackage {
createTurboModulesFactory(ctx: TurboModuleContext): TurboModulesFactory {
return new ImagePickerTurboModulesFactory(ctx);
}
}
153 changes: 153 additions & 0 deletions harmony/image_picker/src/main/ets/ImagePickerTurboModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import fs from '@ohos.file.fs';
import image from '@ohos.multimedia.image';
import wantConstant from '@ohos.app.ability.wantConstant';
import uri from '@ohos.uri'
import util from '@ohos.util';
import { TurboModule } from 'rnoh/ts';
import type { TurboModuleContext } from 'rnoh/ts';
import type Want from '@ohos.app.ability.Want';
import Logger from './Logger'


export type MediaType = 'photo' | 'video' | 'mixed';

export type PhotoQuality = 0 | 0.1 | 0.2 | 0.3 | 0.4 | 0.5 | 0.6 | 0.7 | 0.8 | 0.9 | 1;

export type AndroidVideoOptions = 'low' | 'high';

export type ErrorCode = 'camera_unavailable' | 'permission' | 'others';

export class OptionsCommon {
mediaType: MediaType;
maxWidth?: number;
maxHeight?: number;
quality?: PhotoQuality;
videoQuality?: AndroidVideoOptions;
includeBase64?: boolean;
includeExtra?: boolean;
formatAsMp4?: boolean;
presentationStyle?: 'currentContext' | 'fullScreen' | 'pageSheet' | 'formSheet' | 'popover' | 'overFullScreen' | 'overCurrentContext';
assetRepresentationMode?: 'auto' | 'current' | 'compatible';
}

export class Asset {
base64?: string;
uri?: string;
width?: number;
height?: number;
fileSize?: number;
type?: string;
fileName?: string;
duration?: number;
bitrate?: number;
timestamp?: string;
id?: string;
originalPath?: string;
}

export class ImagePickerResponse {
didCancel?: boolean;
errorCode?: ErrorCode;
errorMessage?: string;
assets?: Asset[];
}

export class AbilityResult {
resultCode: number;
want?: Want;
}


export class ImageLibraryOptions extends OptionsCommon {
selectionLimit?: number;
}


export class ImagePickerTurboModule extends TurboModule {
constructor(protected ctx: TurboModuleContext) {
super(ctx);
}

launchCamera(): void {
Logger.info('launchCamera');
}

launchImageLibrary(options?: ImageLibraryOptions, callback?: (e) => void): void {
let res = this.startAbilityToImage(options.mediaType, options.selectionLimit, wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION)
res.then((e) => {
Logger.info(JSON.stringify(e));
callback(e);
}).catch((e) => {
//返回出错数据
callback(e);
})
}

async startAbilityToImage(type: MediaType, selectionLimit: number, action: wantConstant.Flags | string): Promise<ImagePickerResponse> {
let results: ImagePickerResponse = { assets: [] }
let want = {
"deviceId": "",
"bundleName": "",
"abilityName": "",
"uri": "",
"type": "image/*",
"action": action,
"parameters": {
//singleselect multipleselect
uri: 'multipleselect',
maxSelectCount: selectionLimit,
filterMediaType: type == 'photo' ? 'FILTER_MEDIA_TYPE_IMAGE' : (type == 'video' ? 'FILTER_MEDIA_TYPE_VIDEO' : 'FILTER_MEDIA_TYPE_ALL')
},
"entities": []
}
let result: AbilityResult = await this.ctx.uiAbilityContext.startAbilityForResult(want as Want);
Logger.info(JSON.stringify(result));
let images: Array<string> = result.want.parameters['select-item-list'] as Array<string>
for (let value of images) {
let imgObj: Asset = {}
const mimeUri = new uri.URI(value);
if (mimeUri.scheme === 'file') {
let i = value.lastIndexOf('/')
imgObj.fileName = value.substring(i + 1)
i = value.lastIndexOf('.')
imgObj.type = 'Unknown'
if (i != -1) {
imgObj.type = value.substring(i + 1)
}
}
// console.log(TAG + ',launchImageLibrary,value,' + imgObj.type);
let file = fs.openSync(value, fs.OpenMode.CREATE)
let stat = fs.statSync(file.fd);
imgObj.originalPath = value
try {
let filePath = this.ctx.uiAbilityContext.cacheDir + '/rn_image_picker_lib_temp_' + util.generateRandomUUID(true) + '.' + imgObj.type
fs.copyFileSync(file.fd, filePath, 0)
imgObj.uri = 'file://' + filePath
} catch (e) {
Logger.info('复制到应用缓存区失败!');
//返回出错数据
throw e;
}
// imgObj.isOriginal = result.want.parameters.isOriginal.valueOf()
imgObj.fileSize = stat.size;
if (type === 'photo') {
let imageIS = image.createImageSource(file.fd)
let imagePM = await imageIS.createPixelMap()
let imgInfo = await imagePM.getImageInfo()
// imagePM.getBytesNumberPerRow()
imgObj.height = imgInfo.size.width
imgObj.width = imgInfo.size.height
imagePM.release()
imageIS.release()
}
fs.closeSync(file);
results.assets.push(imgObj)
}
return results;
}
}





40 changes: 40 additions & 0 deletions harmony/image_picker/src/main/ets/Logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import hilog from '@ohos.hilog';

class Logger {
private domain: number;
private prefix: string;
private format: string = '%{public}s, %{public}s';
private isDebug: boolean;

/**
* constructor.
*
* @param Prefix Identifies the log tag.
* @param domain Domain Indicates the service domain, which is a hexadecimal integer ranging from 0x0 to 0xFFFFF.
*/
constructor(prefix: string = 'MyApp', domain: number = 0xFF00, isDebug = false) {
this.prefix = prefix;
this.domain = domain;
this.isDebug = isDebug;
}

debug(...args: string[]): void {
if (this.isDebug) {
hilog.debug(this.domain, this.prefix, this.format, args);
}
}

info(...args: string[]): void {
hilog.info(this.domain, this.prefix, this.format, args);
}

warn(...args: string[]): void {
hilog.warn(this.domain, this.prefix, this.format, args);
}

error(...args: string[]): void {
hilog.error(this.domain, this.prefix, this.format, args);
}
}

export default new Logger('ImagePickerTurboModule', 0xFF00, true)
11 changes: 11 additions & 0 deletions harmony/image_picker/src/main/module.json5
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"module": {
"name": "image_picker",
"type": "har",
"deviceTypes": [
"default",
"tablet",
"2in1"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"string": [
{
"name": "page_show",
"value": "page from npm package"
}
]
}
1 change: 1 addition & 0 deletions harmony/image_picker/ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./src/main/ets/ImagePickerPackage"
Loading

0 comments on commit ea35655

Please sign in to comment.