CommUtils.ets 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2024 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. import { CommonConstants2 } from "./CommonConstants2";
  16. import { image } from "@kit.ImageKit";
  17. import { fileIo } from "@kit.CoreFileKit";
  18. import { transverter, TransverterLanguage, TransverterType } from "@nutpi/chinese_transverter";
  19. export function imagePathToPixelMap(imagePath: string): Promise<image.PixelMap> {
  20. return new Promise((resolve, reject) => {
  21. try {
  22. let file = fileIo.openSync(imagePath, fileIo.OpenMode.READ_ONLY | fileIo.OpenMode.CREATE)
  23. let fd = file.fd;
  24. let imageSource = image.createImageSource(fd);
  25. let opts: image.DecodingOptions = { editable: false };
  26. // 使用ImageSource创建PixelMap
  27. imageSource.createPixelMap(opts).then(pixelMap => {
  28. resolve(pixelMap);
  29. }).catch((error:Error) => {
  30. console.error(`onecold 创建PixelMap失败: ${error.message}`);
  31. reject(error);
  32. });
  33. } catch (error) {
  34. console.error(`处理异常: ${error.message}`);
  35. reject(error);
  36. }
  37. });
  38. }
  39. export function secondToTime(seconds: number): string {
  40. let hourUnit = CommonConstants2.HOUR_UNIT * CommonConstants2.HOUR_UNIT;
  41. let hour: number = Math.floor(seconds / hourUnit);
  42. let minute: number = Math.floor((seconds - hour * hourUnit) / CommonConstants2.HOUR_UNIT);
  43. let second: number = seconds - hour * hourUnit - minute * CommonConstants2.HOUR_UNIT;
  44. let hourStr: string = hour < CommonConstants2.TIME_CONST_TEN ? `0${hour.toString()}` : `${hour.toString()}`
  45. let minuteStr: string = minute < CommonConstants2.TIME_CONST_TEN ? `0${minute.toString()}` : `${minute.toString()}`
  46. let secondStr: string = second < CommonConstants2.TIME_CONST_TEN ? `0${second.toString()}` : `${second.toString()}`
  47. if (hour > 0) {
  48. return `${hourStr}:${minuteStr}:${secondStr}`;
  49. }
  50. if (minute > 0) {
  51. return `${minuteStr}:${secondStr}`;
  52. } else {
  53. return `00:${secondStr}`;
  54. }
  55. }
  56. export function getTransverterText(message: string, transverterType:number):string{
  57. if(transverterType==1){
  58. return transverter({
  59. type: TransverterType.TRADITIONAL,
  60. str: message,
  61. language: TransverterLanguage.ZH_TW
  62. });
  63. }else if(transverterType==2){
  64. return transverter({
  65. type: TransverterType.SIMPLIFIED,
  66. str: message,
  67. language: TransverterLanguage.ZH_CN
  68. });
  69. }else{
  70. return message;
  71. }
  72. }