rntpc_react-native-image-rotate:基于 react-native-image-rotate 的 OpenHarmony 适配版,图片旋转

基于 react-native-image-rotate 的 OpenHarmony 适配版,图片旋转

Branch5Tags7

Document Template: v0.4.2

react-native-image-rotate

This project is based on react-native-image-rotate.

This third-party library has been migrated to GitCode and is now available for direct download from npm, the new package name is: @react-native-ohos/react-native-image-rotate, the version correspondence details are as follows:

Name Version(Npm Address) Release Information Supported RN Version Supported Autolink Compile API Version Community Baseline Version Source code address
@react-native-ohos/react-native-image-rotate ~2.3.0 (In development) GitCode Releases 0.82.* Yes API12+ 2.1.0 br_rnoh0.82
@react-native-ohos/react-native-image-rotate ~2.2.0 GitCode Releases 0.77.* No API12+ 2.1.0 br_rnoh0.77
@react-native-ohos/react-native-image-rotate ~2.1.1 GitCode Releases 0.72.* Yes API12+ 2.1.0 br_rnoh0.72
@react-native-oh-tpl/react-native-image-rotate <= 2.1.0-0.0.3@deprecated Github Releases(deprecated) 0.72.* No API12+ 2.1.0 sig

Introduction

react-native-image-rotate is used to rotate images in React Native applications. It provides the rotateImage static method, which rotates local or remote images by multiples of 90 degrees (negative values indicate counter-clockwise rotation) and returns the URI of the rotated image on success.

Installation

Go to the project directory and execute the following instruction:

npm

npm install @react-native-ohos/react-native-image-rotate

yarn

yarn add @react-native-ohos/react-native-image-rotate
Version Supported Autolink Supported RN Version
~2.3.0 Yes 0.82.*

Projects using AutoLink need to be configured according to this document, AutoLink framework guide: https://gitcode.com/CPF-RN/ohos_react_native/blob/main/docs/en/02-development/02-development-guide/autolinking.md

If the version you are using supports Autolink and the project has integrated Autolink, you can skip the ManualLink configuration.

ManualLink: This step provides guidance for manually configuring native dependencies.

Open the harmony directory of the OpenHarmony project in DevEco Studio.

1. Overrides RN SDK

To ensure the project relies on the same version of the RN SDK, you need to add an overrides field in the project's root oh-package.json5 file, specifying the RN SDK version to be used. The replacement version can be a specific version number, a semver range, or a locally available HAR package or source directory.

For more information about the purpose of this field, please refer to the official documentation.

{
  "overrides": {
    "@rnoh/react-native-openharmony": "~0.82.18" // ohpm version
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony.har" // a locally available HAR package
    // "@rnoh/react-native-openharmony" : "./react_native_openharmony" // source code directory
  }
}

2. Introducing Native Code

Currently, two methods are available:

  • Use the HAR file.
  • Directly link to the source code.

Method 1 (recommended): Use the HAR file.

The HAR file is stored in the `harmony` directory in the installation path of the third-party library.

Open entry/oh-package.json5 file and add the following dependencies:

"dependencies": {
    "@react-native-ohos/react-native-image-rotate": "file:../../node_modules/@react-native-ohos/react-native-image-rotate/harmony/imageRotate.har"
  }

Click the sync button in the upper right corner.

Alternatively, run the following instruction on the terminal:

cd entry
ohpm install

Method 2: Directly link to the source code.

For details, see [Directly Linking Source Code](https://gitcode.com/CPF-RN/usage-docs/blob/master/en/link-source-code.md)

3. Configuring CMakeLists and Introducing ImageRotatePackage

Open entry/src/main/cpp/CMakeLists.txt and add the following code:

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-image-rotate/src/main/cpp" ./imageRotate)
# 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_imageRotate)
# RNOH_END: manual_package_linking_2

Open entry/src/main/cpp/PackageProvider.cpp and add the following code:

#include "RNOH/PackageProvider.h"
#include "SamplePackage.h"
+ #include "ImageRotatePackage.h"

using namespace rnoh;

std::vector<std::shared_ptr<Package>> PackageProvider::getPackages(Package::Context ctx) {
    return {
      std::make_shared<SamplePackage>(ctx),
+     std::make_shared<ImageRotatePackage>(ctx)
    };
}

4. Introducing RNImageRotatePackage to ArkTS

Open the entry/src/main/ets/RNPackagesFactory.ts file and add the following code:

  ...

+  import { RNImageRotatePackage } from '@react-native-ohos/react-native-image-rotate/ts';

export function createRNPackages(ctx: RNPackageContext): RNPackage[] {
  return [
    new SamplePackage(ctx),
+   new RNImageRotatePackage(ctx)
  ];
}

Running

Click the sync button in the upper right corner.

Alternatively, run the following instruction on the terminal:

cd entry
ohpm install

Then build and run the code.

Constraints

Compatibility

This document is verified based on the following versions:

  1. RNOH: 0.82.7; SDK: HarmonyOS 6.0.1 Release SDK; IDE: DevEco Studio 6.0.1 Release; ROM:6.0.0.328 SP26;

Permission Requirements

  • If the image uri is a network URL, the application needs to apply for network permissions.

    Include applicable permissions in the entry/src/main/module.json5 file within the entry directory.

requestPermissions: [
  {
    name: "ohos.permission.INTERNET",
  },
],

API requirements

All versions of the current third-party libraries have implemented version isolation, supporting compilation in `API12+` projects and execution on `API12+` ROMs.

Example

The following code shows the basic use scenario of the repository:

The name of the imported repository remains unchanged.

import React, { Component } from 'react';
import {
  Image,
  StyleSheet,
  Text,
  TouchableHighlight,
  View
} from 'react-native';

import ImageRotate from 'react-native-image-rotate';

const SOURCE_IMAGE = 'https://upload.wikimedia.org/wikipedia/en/5/56/Warcraft_Teaser_Poster.jpg';

export default class App extends Component {

  constructor() {
    super();
    this.state = {
      image: SOURCE_IMAGE,
      currentAngle: 0,
      width: 150,
      height: 240,
    };

    this.rotate = this.rotate.bind(this);
  }

  rotate(angle) {
    const nextAngle = this.state.currentAngle + angle;
    ImageRotate.rotateImage(
      SOURCE_IMAGE,
      nextAngle,
      (uri) => {
        this.setState({
          image: uri,
          currentAngle: nextAngle,
          width: this.state.height,
          height: this.state.width,
        });
      },
      (error) => {
        console.error(error);
      }
    );
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={styles.imageContainer}>
          <Image style={{width: this.state.width, height: this.state.height}} source={{uri: this.state.image}} />
        </View>
        <TouchableHighlight
          onPress={() => this.rotate(90)}
          style={styles.button}
        >
          <Text style={styles.text}>ROTATE CW</Text>
        </TouchableHighlight>

        <TouchableHighlight
          onPress={() => this.rotate(-90)}
          style={styles.button}
        >
          <Text style={styles.text}>ROTATE CCW</Text>
        </TouchableHighlight>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  button: {
    backgroundColor: 'red',
    padding: 5,
    margin: 5,
  },
  imageContainer: {
    height: 240,
    justifyContent: 'center',
  },
  text: {
    color: 'white',
  },
});

How to Use

Rotate Image

Call the ImageRotate.rotateImage static method to rotate an image. uri supports both local paths and network URLs (remote images are downloaded automatically). angle is the rotation angle and must be a multiple of 90 (negative values indicate counter-clockwise rotation). The success callback returns the URI of the rotated image, and the failure callback returns the error information.

ImageRotate.rotateImage(
  uri,
  90,
  (rotatedUri) => {
    // Rotation succeeded, rotatedUri is the URI of the rotated image
  },
  (error) => {
    // Rotation failed
  }
);

Available APIs

The **Platform** column indicates the platform where the properties are supported in the original third-party library.

If the value of **OpenHarmony Platform Support** is **yes**, it means that the OpenHarmony platform supports this property; **no** means the opposite; **partially** means some capabilities of this property are supported. The usage method is the same on different platforms and the effect is the same as that of iOS or Android.

API

Name Type Parameter Type Return Value Required Platform OpenHarmony Platform Support Description
rotateImage function uri: string, angle: number, success: (uri: string) => void, failure: (error: Object) => void void no iOS/Android Yes Rotates an image. Only angles that are multiples of 90 are supported, negative angles indicate counter-clockwise rotation; the success callback returns the URI of the rotated image, and the failure callback returns the error information.

Known Issues

Other

None

Directory Structure

/rntpc_react-native-image-rotate  # Project root
├── harmony/
│   ├── imageRotate/
│   └── imageRotate.har
├── src/
│   └── NativeImageRotate.ts
├── index.js
├── LICENSE  # License file
├── OAT.xml  # OAT self-check configuration
├── package.json  # Package configuration
├── README_en.md  # English installation and usage guide
├── README.md  # Chinese installation and usage guide
└── RNImageRotate.podspec

How to Contribute

If you find any problem when using react-native-image-rotate, submit an issue or a PR.

License

The repository is based on The MIT License (MIT).

Introduction

基于 react-native-image-rotate 的 OpenHarmony 适配版,图片旋转

Customize your domain