TimeExperienceCustomContainer.ets 5.3 KB
Newer Older
T
tianwenzhe 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*
 * Copyright (c) 2022-2023 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 prompt from '@ohos.prompt';
import fileio from '@ohos.fileio';
import router from '@ohos.router';
import screenshot from '@ohos.screenshot';
import image from '@ohos.multimedia.image';
import Logger from '../model/Logger';
import mediaLibrary from '@ohos.multimedia.mediaLibrary';

let path = globalThis.dir;

const TAG = '[Screenshot]';
@Component
export struct CustomContainer {
  @Link name : string;
  title : string = '';
  StepTips: string = '';
  Url : string = '';
  @Link StartEnable : boolean
  @Link Vue : boolean;
  @Link time : string;
  @BuilderParam content: () => void;
  @Builder
  PassBtn(text: Resource, isFullScreen: boolean) {
    if(this.Vue == false){
      Button({stateEffect:this.Vue}) {
        Image($r('app.media.ic_public_pass')).width('20vp').height('20vp')
      }.width('30%').height('30vp').backgroundColor(Color.Grey).opacity(0.4)
      .onClick(()=>{

      })
    }
    else{
      Button({stateEffect:this.Vue}) {
        Image($r('app.media.ic_public_pass')).width('20vp').height('20vp')
      }.width('30%').height('30vp').backgroundColor(Color.Grey)
      .onClick(()=>{
        router.back({
          url:this.Url,
T
tianwenzhe 已提交
53
          params: {result : 'true', title : this.name,
T
tianwenzhe 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
          }
        })
        this.getScreen(isFullScreen);
        prompt.showToast({
          message: '通过', duration: 1000
        });
      })
    }
  }
  @Builder
  FailBtn(text: Resource, isFullScreen: boolean) {
    Button(){
      Image($r('app.media.ic_public_fail')).width('20vp').height('20vp')
    }.width('30%').height('30vp').backgroundColor(Color.Grey)
    .onClick(()=>{
      router.back({
        url:this.Url,
T
tianwenzhe 已提交
71
        params: {result : 'false',title : this.name,
T
tianwenzhe 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84
        }
      })
      this.getScreen(isFullScreen);
      prompt.showToast({
        message: '失败', duration: 1000
      });
    })
  }
  build() {
    Column() {
      Row() {
        Button(){
          Image($r('app.media.ic_public_back')).width('20vp').height('18vp').margin({left:'20vp'})
85
        }.backgroundColor(Color.Black).size({ width: '40vp', height: '30vp' })
T
tianwenzhe 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        .onClick(()=>{
          router.back({
            url:this.Url,
            params: {result : 'None',}
          })
        })
        Text(this.title).fontColor(Color.White).fontSize('18fp').margin({left:'-20vp'})
        Text('hello').fontColor(Color.White).visibility(Visibility.Hidden)
      }.backgroundColor(Color.Black).height('10%').width('100%').justifyContent(FlexAlign.SpaceBetween)
      this.content();
      Blank()
      Row() {
        this.PassBtn($r('app.string.btn_fullscreen'), true);
        Button(){
          Image($r('app.media.ic_public_help')).width('20vp').height('20vp')
        }.width('30%').height('30vp').backgroundColor(Color.Grey)
        .onClick(() =>{
          AlertDialog.show({
            title:'操作提示',
            message: this.StepTips,
            confirm:{
              value:'OK',
              action:()=>{
              }
            }
          })
        })
        this.FailBtn($r('app.string.btn_fullscreen'), true);
      }.width('100%').justifyContent(FlexAlign.SpaceEvenly).backgroundColor(Color.Black)
    }.height('98%').width('100%')
  }
  async savePicture(data: image.PixelMap, context: any) {
    Logger.info(TAG, `savePicture`);
    let packOpts: image.PackingOption = {
      format: "image/jpeg", quality: 100
    };
    let info = {
      prefix: 'IMG_', suffix: '.jpg', directory: mediaLibrary.DirectoryType.DIR_IMAGE
    };
    let name = this.name;
    let displayName = `${info.prefix}${name}${info.suffix}`;
    let dirPath = path + '/screenshot' + '/' + displayName;
    let imagePackerApi = image.createImagePacker();
    let arrayBuffer = await imagePackerApi.packing(data, packOpts);
    let fd = fileio.openSync(dirPath,0o102,0o666);
    imagePackerApi.release();
    try {
      await fileio.write(fd, arrayBuffer);
    } catch (err) {
      Logger.error(`write failed, code is ${err.code}, message is ${err.message}`);
    }
    await fileio.close(fd);
    Logger.info(TAG, `write done`);

  }
  getScreen = (isFullScreen: boolean) => {
    let screenshotOptions: screenshot.ScreenshotOptions = {
      screenRect: { left: 0, top: 0, width: 400, height: 400 },
      imageSize: { width: 400, height: 400 },
      rotation: 0,
      displayId: 0
    };
    if (isFullScreen) {
      screenshotOptions = {
        rotation: 0
      }
    }
    try {
      screenshot.save(screenshotOptions, (err, data: image.PixelMap) => {
        if (err) {
          Logger.info(TAG, `Failed to save the screenshot. Error:${JSON.stringify(err)}`);
        }
        Logger.info(TAG, 'save callback');
        this.savePicture(data, getContext(this) as any);
      })
    } catch (err) {
      Logger.error(`save failed, code is ${err.code}, message is ${err.message}`);
    }
  }
}