| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- /*
- * Copyright (c) 2024 Huawei Device Co., Ltd.
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- import { CommonConstants2 } from "./CommonConstants2";
- import { image } from "@kit.ImageKit";
- import { fileIo } from "@kit.CoreFileKit";
- import { transverter, TransverterLanguage, TransverterType } from "@nutpi/chinese_transverter";
- export function imagePathToPixelMap(imagePath: string): Promise<image.PixelMap> {
- return new Promise((resolve, reject) => {
- try {
- let file = fileIo.openSync(imagePath, fileIo.OpenMode.READ_ONLY | fileIo.OpenMode.CREATE)
- let fd = file.fd;
- let imageSource = image.createImageSource(fd);
- let opts: image.DecodingOptions = { editable: false };
- // 使用ImageSource创建PixelMap
- imageSource.createPixelMap(opts).then(pixelMap => {
- resolve(pixelMap);
- }).catch((error:Error) => {
- console.error(`onecold 创建PixelMap失败: ${error.message}`);
- reject(error);
- });
- } catch (error) {
- console.error(`处理异常: ${error.message}`);
- reject(error);
- }
- });
- }
- export function secondToTime(seconds: number): string {
- let hourUnit = CommonConstants2.HOUR_UNIT * CommonConstants2.HOUR_UNIT;
- let hour: number = Math.floor(seconds / hourUnit);
- let minute: number = Math.floor((seconds - hour * hourUnit) / CommonConstants2.HOUR_UNIT);
- let second: number = seconds - hour * hourUnit - minute * CommonConstants2.HOUR_UNIT;
- let hourStr: string = hour < CommonConstants2.TIME_CONST_TEN ? `0${hour.toString()}` : `${hour.toString()}`
- let minuteStr: string = minute < CommonConstants2.TIME_CONST_TEN ? `0${minute.toString()}` : `${minute.toString()}`
- let secondStr: string = second < CommonConstants2.TIME_CONST_TEN ? `0${second.toString()}` : `${second.toString()}`
- if (hour > 0) {
- return `${hourStr}:${minuteStr}:${secondStr}`;
- }
- if (minute > 0) {
- return `${minuteStr}:${secondStr}`;
- } else {
- return `00:${secondStr}`;
- }
- }
- export function getTransverterText(message: string, transverterType:number):string{
- if(transverterType==1){
- return transverter({
- type: TransverterType.TRADITIONAL,
- str: message,
- language: TransverterLanguage.ZH_TW
- });
- }else if(transverterType==2){
- return transverter({
- type: TransverterType.SIMPLIFIED,
- str: message,
- language: TransverterLanguage.ZH_CN
- });
- }else{
- return message;
- }
- }
|