Signed-off-by: maojingling <maojingling@huawei.com>
This commit is contained in:
maojingling 2024-04-01 17:16:00 +08:00
parent b9a8e66037
commit 044dcadf1d
8 changed files with 46 additions and 41 deletions

View File

@ -4,7 +4,7 @@
{
"name": "default",
"signingConfig": "release",
"compileSdkVersion": 10,
"compileSdkVersion": 12,
"compatibleSdkVersion": 9
}
],

View File

@ -31,7 +31,7 @@ export default class Log {
* @param {string} tag - Page or class tag
* @param {string} log - Log needs to be printed
*/
static showInfo(tag, ...args: any[]) {
static showInfo(tag: string, ...args: string[]) {
hiLog.info(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
}
@ -41,7 +41,7 @@ export default class Log {
* @param {string} tag - Page or class tag
* @param {string} log - Log needs to be printed
*/
static showDebug(tag, ...args: any[]) {
static showDebug(tag: string, ...args: string[]) {
hiLog.debug(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
}
@ -51,8 +51,7 @@ export default class Log {
* @param {string} tag - Page or class tag
* @param {string} log - Log needs to be printed
*/
static showError(tag, ...args: any[]) {
static showError(tag: string, ...args: string[]) {
hiLog.error(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
}
}

View File

@ -13,6 +13,7 @@
* limitations under the License.
*/
import { BusinessError } from '@ohos.base';
import ScreenshotManager from '@ohos.screenshot';
import WindowMar from '@ohos.window';
import ImageMar from '@ohos.multimedia.image';
@ -21,6 +22,7 @@ import FileIo from '@ohos.fileio';
import display from '@ohos.display';
import Constants from '../common/constants';
import Log from '../../../../../../../../common/src/main/ets/default/Log';
import Want from '@ohos.app.ability.Want';
const TAG = 'ScreenShot-ScreenShotModel';
@ -32,35 +34,35 @@ const OPTIONS_QUALITY = 100;
const CREATE_WINDOW_DELAY = 300;
export class ScreenShotModel {
private captureImage: ImageMar.PixelMap;
private captureImage: ImageMar.PixelMap | undefined = undefined;
private imageFileName: string = '';
async shotScreen() {
Log.showInfo(TAG, 'shotScreen');
await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
await new Promise<number>((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
ScreenshotManager.save().then(async (data) => {
Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`)
Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`);
if (!!data) {
this.captureImage = data;
AppStorage.Set('captureImage', data);
this.saveImage(this.captureImage, {
format: ['image/jpeg'],
format: 'image/jpeg',
quality: OPTIONS_QUALITY,
});
}
}).catch((err) => {
}).catch((err: BusinessError) => {
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`);
})
});
}
async saveImage(pixelMap, options) {
async saveImage(pixelMap: ImageMar.PixelMap, options: ImageMar.PackingOption) {
const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext);
Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`);
this.imageFileName = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE;
var packer = ImageMar.createImagePacker();
const packer = ImageMar.createImagePacker();
Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`);
this.showWindow();
await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
await new Promise<number>((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
packer.packing(pixelMap, options).then((jpegData) => {
Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData);
media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => {
@ -80,17 +82,17 @@ export class ScreenShotModel {
Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`);
fileAsset.close(fd).then(() => {
Log.showInfo(TAG, 'FileIo close,successful');
})
});
});
});
} else {
Log.showInfo(TAG, 'createAsset fail');
}
})
});
} else {
Log.showInfo(TAG, 'getPublicDirectory fail');
}
})
});
});
}
@ -99,24 +101,24 @@ export class ScreenShotModel {
WindowMar.find(Constants.WIN_NAME).then((win) => {
win.show(() => {
Log.showInfo(TAG, 'window show');
})
})
});
});
}
dismiss(): void {
Log.showInfo(TAG, 'dismiss');
//close ability
globalThis.shotScreenContext.terminateSelf((err, data) => {
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`);
globalThis.shotScreenContext.terminateSelf((err: BusinessError) => {
Log.showInfo(TAG, `terminateSelf finish err:${err}`);
WindowMar.find(Constants.WIN_NAME).then((win) => {
win.destroy(() => {
Log.showInfo(TAG, 'destroy the window finish');
})
})
})
});
});
});
}
openAbility(wantData) {
openAbility(wantData: Want) {
Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`);
globalThis.shotScreenContext.startAbility(wantData);
}

View File

@ -19,6 +19,8 @@ import display from '@ohos.display';
import Log from '../../../../../../common/src/main/ets/default/Log';
import Constants from '../../../../../../features/screenshot/src/main/ets/com/ohos/common/constants';
import ScreenShotModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel';
import Want from '@ohos.app.ability.Want';
import { BusinessError } from '@ohos.base';
const TAG = 'ScreenShot-ScreenShotServiceAbility';
const INDEX_PAGE = 'pages/index';
@ -26,10 +28,10 @@ const ZOOM_RATIO = 0.4;
const WINDOW_Y = 300;
class ServiceExtAbility extends ServiceExtensionAbility {
onCreate(want): void {
onCreate(want: Want): void {
Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
globalThis.shotScreenContext = this.context;
const windowConfig = {
const windowConfig: windowManager.Configuration = {
name: Constants.WIN_NAME,
windowType: windowManager.WindowType.TYPE_SCREENSHOT,
ctx: this.context,
@ -48,7 +50,7 @@ class ServiceExtAbility extends ServiceExtensionAbility {
});
});
});
}, (error) => {
}, (error: BusinessError) => {
Log.showInfo(TAG, 'window createFailed, error.code = ' + error.code);
});
Log.showInfo(TAG, 'after window create');

View File

@ -12,6 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Log from '../../../../../../common/src/main/ets/default/Log';
import Constants from '../common/constants';
import ViewModel from '../vm/ViewModel';
@ -28,16 +29,16 @@ struct Index {
Stack({ alignContent: Alignment.Bottom }) {
Column() {
Image(this.captureImage)
.border({ color: Color.White, radius: 8, width: 8 })
.border({ color: Color.White, radius: 8, width: 8 });
}
.width(Constants.FULL_CONTAINER_WIDTH)
.height(Constants.FULL_CONTAINER_HEIGHT)
.onClick(() => {
ViewModel.StartPhotosAbility(this.imageFilename);
})
});
}
.width(Constants.FULL_CONTAINER_WIDTH)
.height(Constants.FULL_CONTAINER_HEIGHT)
.height(Constants.FULL_CONTAINER_HEIGHT);
}
aboutToAppear() {

View File

@ -15,29 +15,30 @@
import Log from '../../../../../../common/src/main/ets/default/Log';
import ShotScreenModel from '../../../../../../features/screenshot/src/main/ets/com/ohos/model/screenShotModel';
import Want from '@ohos.app.ability.Want';
const TAG = 'ScreenShot-ViewModel'
const GALLERY_BUNDLE = 'com.ohos.photos'
const GALLERY_ABILITY = 'com.ohos.photos.MainAbility'
const TAG = 'ScreenShot-ViewModel';
const GALLERY_BUNDLE = 'com.ohos.photos';
const GALLERY_ABILITY = 'com.ohos.photos.MainAbility';
export class ViewModel {
constructor() {
this.ViewModelInit()
this.ViewModelInit();
}
ViewModelInit(): void {
Log.showInfo(TAG, 'ViewModelInit');
}
StartPhotosAbility(imageFilename): void {
Log.showInfo(TAG, `StartPhotosAbility imageFilename:${imageFilename}`);
let wantData = {
StartPhotosAbility(imageFileName: string): void {
Log.showInfo(TAG, `StartPhotosAbility imageFileName:${imageFileName}`);
const wantData: Want = {
bundleName: GALLERY_BUNDLE,
abilityName: GALLERY_ABILITY,
parameters: {
uri: imageFilename
uri: imageFileName
}
}
};
ShotScreenModel.openAbility(wantData);
//close window
ShotScreenModel.dismiss();

View File

@ -26,7 +26,7 @@
"permissions": [
"ohos.permission.CAPTURE_SCREEN"
],
"srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ts",
"srcEntrance": "./ets/ServiceExtAbility/ServiceExtAbility.ets",
"name": "com.ohos.screenshot.ServiceExtAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",