HarmonyOS 图片操作,合成后导出base64

需要做图片合成操作,例二维码中间加logo,在背景图片特定位置加二维码和文字等,合成后导出base64给RN使用,请专家指导用哪些api,如何开发?

HarmonyOS
2024-11-13 09:36:35
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
superinsect

​可以使canvas的drawImage方法实现该场景:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-drawing-customization-on-canvas-V5#ZH-CN_TOPIC_0000001847051164__画布组件常用方法

https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-canvasrenderingcontext2d-V5#ZH-CN_TOPIC_0000001847052356__drawimage

参考demo实现:

EntryAbility.ets​

import AbilityConstant from '@ohos.app.ability.AbilityConstant'; 
import hilog from '@ohos.hilog'; 
import UIAbility from '@ohos.app.ability.UIAbility'; 
import Want from '@ohos.app.ability.Want'; 
import window from '@ohos.window'; 
 
export default class EntryAbility extends UIAbility { 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
    windowStage.loadContent('pages/Index', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
  } 
 
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
  } 
 
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
}

Index.ets

@Entry 
@Component 
struct GetImageData { 
  private settings: RenderingContextSettings = new RenderingContextSettings(true) 
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 
  private offCanvas: OffscreenCanvas = new OffscreenCanvas(600, 600) 
  private img:ImageBitmap = new ImageBitmap("/common/images/666.png") 
  private img2:ImageBitmap = new ImageBitmap("/common/images/90.jpg"); 
  @State ima:PixelMap|undefined=undefined; 
 
  build() { 
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
      Canvas(this.context) 
        .width('100%') 
        .height('100%') 
        .backgroundColor('#F5DC62') 
        .onReady(() =>{ 
          let offContext = this.offCanvas.getContext("2d", this.settings) 
          // 使用drawImage接口将图片画在(0,0)为起点,宽高100的区域 
          offContext.drawImage(this.img,0,0,100,100); 
          // 使用drawImage接口将图片画在(0,0)为起点,宽高20的区域 
          offContext.drawImage(this.img2,40,40,20,20); 
          // 使用getImageData接口,获得canvas组件区域中,(50,50)为起点,宽高130范围内的绘制内容 
          //let imagedata = offContext.getImageData(50,50,130,130); 
          let imagedata = offContext.getImageData(50,50,130,130); 
 
          this.ima=offContext.getPixelMap(0,0,100,100); 
          // 使用putImageData接口将得到的ImageData画在起点为(150, 150)的区域中 
          // offContext.putImageData(imagedata,150,150); 
          // 将离屏绘制的内容画到canvas组件上 
          let image = this.offCanvas.transferToImageBitmap(); 
          this.context.transferFromImageBitmap(image); 
        }) 
      Image(this.ima) 
        .width(100).height(100) 
 
    } 
    .width('100%') 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-11-13 17:56:56
相关问题
HarmonyOS 图片压缩和图片base64
269浏览 • 1回复 待解决
HarmonyOS 图片进行base64编码报错
684浏览 • 1回复 待解决
HarmonyOS Image如何显示base64图片
441浏览 • 1回复 待解决
HarmonyOS如何将图片Base64
905浏览 • 1回复 待解决
怎么把PixelMap图片BASE64
9411浏览 • 1回复 待解决
如何base64转换成图片
10314浏览 • 2回复 待解决
js canvas转base64编码图片不全
10797浏览 • 1回复 待解决
HarmonyOS base64编码问题
632浏览 • 1回复 待解决
HarmonyOS base64解码报错
1040浏览 • 1回复 待解决
HarmonyOS 图片转为base64字符串
176浏览 • 1回复 待解决
HarmonyOS 如何将base64数据转换为图片
505浏览 • 1回复 待解决
HarmonyOS base64编码的API
186浏览 • 1回复 待解决
HarmonyOS arraybuffer转base64乱码
152浏览 • 1回复 待解决
HarmonyOS string怎么base64和decodeBase64
15浏览 • 1回复 待解决
HarmonyOS 中文base64加密解密乱码
561浏览 • 1回复 待解决
base64字符串如何转为图片并保存
2659浏览 • 1回复 待解决
arkts中怎么实现base64编码?
820浏览 • 1回复 待解决
PixelMap怎么转Base64?(非Java)
3335浏览 • 2回复 待解决
如何将图片base64字符串转PixelMap?
594浏览 • 1回复 待解决