chendeben 1 anno fa
parent
commit
6d5230797b

+ 67 - 1
entry/src/main/ets/common/util/ImageUtils.ets

@@ -22,7 +22,7 @@ import { BusinessError } from '@kit.BasicServicesKit';
 import ResponseCode from '@ohos.net.http';
 import { photoAccessHelper } from '@kit.MediaLibraryKit';
 import fs from '@ohos.file.fs';
-import { FileUtil, MD5, PreferencesUtil } from '@pura/harmony-utils';
+import { Base64Util, FileUtil, LogUtil, MD5, PreferencesUtil } from '@pura/harmony-utils';
 import MediaTable from './MediaTable';
 
 const TAG = 'ImageUtils';
@@ -270,6 +270,72 @@ class ImageUtils {
 
 
   }
+  public  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);
+      }
+    });
+  }
+
+  /**
+   * PixelMap转图片base64字符串
+   * @param pixelMap
+   * @param format 目标格式,默认png,当前只支持jpg、webp和png。当传入的格式与文件格式不匹配,可能会导致生成错误的Base64字符串。
+   * @returns
+   */
+  public  async pixelMapToBase64StrBig(pixelMap: image.PixelMap|undefined, format: string = 'image/png'): Promise<string> {
+    if(pixelMap === undefined)
+      return ''
+    try {
+      let packOpts: image.PackingOption = { format: format, quality: 98 }
+      const arrayBuffer = await ImageUtils.packingFromPixelMap(pixelMap, packOpts);
+      let base64Str: string = Base64Util.encodeToStrSync(new Uint8Array(arrayBuffer));
+      let headStr = `data:${format};base64,`;
+      if (!base64Str.startsWith(headStr)) {
+        base64Str = headStr + base64Str
+      }
+      return base64Str;
+    } catch (err) {
+      let error = err as BusinessError;
+      LogUtil.error(`ImageUtil-pixelMapToBase64Str-异常 ~ code: ${error.code} -·- message: ${error.message}`);
+      return '';
+    }
+  }
+
+  /**
+   * 图片压缩或重新打包,使用Promise形式返回结果。
+   * @param source PixelMap-打包的PixelMap资源。
+   * @param options 设置打包参数:
+   *   format 目标格式。当前只支持"image/jpeg"、"image/webp"、"image/png"和"image/heif"12+(不同硬件设备支持情况不同)。
+   *   quality JPEG编码中设定输出图片质量的参数,取值范围为0-100。
+   *   bufferSize 接收编码数据的缓冲区大小,单位为Byte。默认为10MB。bufferSize需大于编码后图片大小。
+   * @returns
+   */
+  static packingFromPixelMap(source: image.PixelMap, options: image.PackingOption): Promise<ArrayBuffer> {
+    const imagePacker: image.ImagePacker = image.createImagePacker();
+    return imagePacker.packToData(source, options).finally(() => {
+      imagePacker.release(); //释放
+    });
+  }
 }
 
 export default new ImageUtils();

+ 3 - 2
entry/src/main/ets/common/util/Utility.ets

@@ -25,6 +25,7 @@ import { VipData } from '../../viewmodel/VipData';
 import { LocalMusic } from '../../view/LocalMusic';
 import { VipPage } from '../../pages/VipPage';
 import { FFmpeg, FFProgressMessageParser } from '@sj/ffmpeg';
+import ImageUtils from './ImageUtils';
 
 interface FFMpegTags {
   album?: string;
@@ -539,7 +540,7 @@ export class Utility {
     }else{
       pixelMap = await Utility.getFetchFrameByTime(uri)
     }
-    return  ImageUtil.pixelMapToBase64StrBig(pixelMap)
+    return  ImageUtils.pixelMapToBase64StrBig(pixelMap)
   }
 
   static async getFilePixelMap(uri:string){
@@ -1186,7 +1187,7 @@ export class Utility {
 
   static getMusisBg():Resource {
 
-    let index = RandomUtil.randomNumber(0,9)
+    let index = RandomUtil.getRandomNumber(0,9)
     LogUtil.info('getMusisBg index = '+index)
     return CommonConstants.musicBgList[index]
 

+ 2 - 1
entry/src/main/ets/controller/AvSessionController.ets

@@ -24,6 +24,7 @@ import { image } from '@kit.ImageKit';
 import { SettingPage } from '../pages/SettingPage';
 import { Utility } from '../common/util/Utility';
 import LyricUtil from '../common/util/LyricUtil';
+import ImageUtils from '../common/util/ImageUtils';
 
 const TAG = 'onecold AvSessionController';
 
@@ -138,7 +139,7 @@ export class AvSessionController {
 
       let imagePixMap: PixelMap|string ;
       if(pixelMapPath){
-        imagePixMap = await ImageUtil.imagePathToPixelMap(pixelMapPath)
+        imagePixMap = await ImageUtils.imagePathToPixelMap(pixelMapPath)
       }else{
         imagePixMap = await ImageUtil.getPixelMapFromMedia($r('app.media.ic_avatar2'));
       }

+ 1 - 1
entry/src/main/ets/view/LocalMusic.ets

@@ -6693,7 +6693,7 @@ export struct LocalMusic {
     if (cover === undefined) {
       return;
     }
-    ImageUtil.imagePathToPixelMap(cover).then((value) => {
+    ImageUtils.imagePathToPixelMap(cover).then((value) => {
       let headFilter = effectKit.createEffect(value);
       if (headFilter !== null) {
         headFilter.blur(15);

+ 13 - 1
lib/src/main/ets/LyricController.ets

@@ -26,6 +26,7 @@ export class LyricController {
     private animDuration: number = DEFAULT_ANIM_DURATION
     private cacheSize: number = DEFAULT_CACHE_SIZE
     private isBold: boolean = true
+    private isSingleLine: boolean = false
     private emptyHint: string = "--"
     private alignMode: "left" | "center" = "center"
     private isHightLightCenter: boolean = true
@@ -190,6 +191,17 @@ export class LyricController {
         return this
     }
 
+    getSingleLine(): boolean {
+        return this.isSingleLine
+    }
+
+
+    setSingleLine(isSingle: boolean): LyricController {
+        this.isSingleLine = isSingle
+        this.onInvalidated(false)
+        return this
+    }
+
     /**
      * The bold style of focused lyric.
      * @returns True to set text bold style, false to set normal style.
@@ -320,4 +332,4 @@ export class LyricController {
     invalidate() {
         this.onInvalidated(false)
     }
-}
+}

+ 12 - 3
lib/src/main/ets/bean/LyricLine.ts

@@ -1,19 +1,28 @@
+import { LyricWord } from './LyricWord'
 /**
  * The line info of lyric.
  */
 export class LyricLine {
-    readonly text: string
+    text: string
     readonly beginTime: number
     nextTime: number
 
+    words: LyricWord[] = []
     /**
      * @param text The text of lyric line.
      * @param beginTime The begin timestamp of this lyric line.
      * @param nextTime The begin timestamp of the next lyric line.
+     * @param words The words array for word-by-word lyrics.
      */
-    constructor(text: string, beginTime: number, nextTime: number) {
+    constructor(text: string, beginTime: number, nextTime: number, words?: LyricWord[]) {
         this.text = text
         this.beginTime = beginTime
         this.nextTime = nextTime
+        this.words = words || []
     }
-}
+
+    // 新增方法:判断是否有逐字歌词
+    hasWords(): boolean {
+        return this.words.length > 0
+    }
+}