mirror of
https://gitee.com/openharmony/applications_screenshot
synced 2024-11-26 16:50:22 +00:00
commit
bddb299916
@ -1,6 +1,4 @@
|
||||
{
|
||||
"apiType": 'stageMode',
|
||||
"buildOption": {
|
||||
"arkEnable": true
|
||||
}
|
||||
"buildOption": {}
|
||||
}
|
@ -13,21 +13,26 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import hiLog from '@ohos.hilog';
|
||||
|
||||
const TAG = 'ScreenShot-Default';
|
||||
const DOMAIN = 0x55EE;
|
||||
const FORMAT = '%{public}s';
|
||||
const PREFIX = '[Screenshot]';
|
||||
const SEPARATOR = ' ';
|
||||
|
||||
/**
|
||||
* Basic log class
|
||||
*/
|
||||
export default class Log {
|
||||
|
||||
/**
|
||||
* print info level log
|
||||
*
|
||||
* @param {string} tag - Page or class tag
|
||||
* @param {string} log - Log needs to be printed
|
||||
*/
|
||||
static showInfo(tag, log) {
|
||||
console.info(`${TAG} tag: ${tag} --> ${log}`);
|
||||
static showInfo(tag, ...args: any[]) {
|
||||
hiLog.info(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -36,8 +41,8 @@ export default class Log {
|
||||
* @param {string} tag - Page or class tag
|
||||
* @param {string} log - Log needs to be printed
|
||||
*/
|
||||
static showDebug(tag, log) {
|
||||
console.debug(`${TAG} tag: ${tag} --> ${log}`);
|
||||
static showDebug(tag, ...args: any[]) {
|
||||
hiLog.debug(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -46,7 +51,8 @@ export default class Log {
|
||||
* @param {string} tag - Page or class tag
|
||||
* @param {string} log - Log needs to be printed
|
||||
*/
|
||||
static showError(tag, log) {
|
||||
console.error(`${TAG} tag: ${tag} --> ${log}`);
|
||||
static showError(tag, ...args: any[]) {
|
||||
hiLog.error(DOMAIN, PREFIX, FORMAT, `tag: ${tag} --> ${args.join(SEPARATOR)}`);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,5 @@
|
||||
*/
|
||||
|
||||
export default class Constants {
|
||||
|
||||
static WIN_NAME = "ScreenShotWindow"
|
||||
|
||||
static WIN_NAME = 'ScreenShotWindow';
|
||||
}
|
@ -12,103 +12,104 @@
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
import Log from '../../../../../../../../common/src/main/ets/default/Log';
|
||||
|
||||
import ScreenshotManager from '@ohos.screenshot';
|
||||
import WindowMar from '@ohos.window';
|
||||
import WindowMar from '@ohos.window';
|
||||
import ImageMar from '@ohos.multimedia.image';
|
||||
import mediaLibrary from '@ohos.multimedia.mediaLibrary';
|
||||
import FileIo from '@ohos.fileio';
|
||||
import Constants from '../common/constants';
|
||||
import display from '@ohos.display';
|
||||
import Constants from '../common/constants';
|
||||
import Log from '../../../../../../../../common/src/main/ets/default/Log';
|
||||
|
||||
const TAG = 'ScreenShot-ScreenShotModel';
|
||||
|
||||
const SCREEN_SHOT_PATH = "Screenshots/";
|
||||
const SCREENSHOT_PREFIX = "Screenshot";
|
||||
const PICTURE_TYPE = ".jpg";
|
||||
const SCREEN_SHOT_PATH = 'Screenshots/';
|
||||
const SCREENSHOT_PREFIX = 'Screenshot';
|
||||
const PICTURE_TYPE = '.jpg';
|
||||
const SAVE_IMAGE_DELAY = 300;
|
||||
const OPTIONS_QUALITY = 100;
|
||||
const CREATE_WINDOW_DELAY = 300;
|
||||
|
||||
export class ScreenShotModel {
|
||||
captureImage: any;
|
||||
captureImage: ImageMar.PixelMap;
|
||||
imageFilename: string = '';
|
||||
|
||||
async shotScreen() {
|
||||
Log.showInfo(TAG, `shotScreen`)
|
||||
Log.showInfo(TAG, 'shotScreen');
|
||||
await new Promise((resolve) => setTimeout(resolve, CREATE_WINDOW_DELAY));
|
||||
ScreenshotManager.save().then(async (data) => {
|
||||
Log.showInfo(TAG, `ScreenshotManager.save data:${JSON.stringify(data)}`)
|
||||
if (!!data) {
|
||||
this.captureImage = data;
|
||||
AppStorage.Set("captureImage", data);
|
||||
AppStorage.Set('captureImage', data);
|
||||
this.saveImage(this.captureImage, {
|
||||
format: ["image/jpeg"], quality: OPTIONS_QUALITY
|
||||
format: ['image/jpeg'],
|
||||
quality: OPTIONS_QUALITY,
|
||||
});
|
||||
}
|
||||
}).catch((err) => {
|
||||
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`)
|
||||
Log.showInfo(TAG, `ScreenshotManager.save err:${JSON.stringify(err)}`);
|
||||
})
|
||||
}
|
||||
|
||||
async saveImage(pixelMap, options) {
|
||||
const media = mediaLibrary.getMediaLibrary(globalThis.shotScreenContext);
|
||||
Log.showInfo(TAG, `saveImage options:${JSON.stringify(options)}`);
|
||||
this.imageFilename = SCREENSHOT_PREFIX + "_" + (new Date()).getTime() + PICTURE_TYPE;
|
||||
this.imageFilename = SCREENSHOT_PREFIX + '_' + (new Date()).getTime() + PICTURE_TYPE;
|
||||
var packer = ImageMar.createImagePacker();
|
||||
Log.showInfo(TAG, `saveImage packer:${JSON.stringify(packer)}`);
|
||||
this.showWindow();
|
||||
await new Promise((resolve) => setTimeout(resolve, SAVE_IMAGE_DELAY));
|
||||
packer.packing(pixelMap, options).then((jpegData) => {
|
||||
Log.showInfo(TAG, `packing jpegData type:` + jpegData.constructor.name + ' value : ' + jpegData);
|
||||
Log.showInfo(TAG, 'packing jpegData type:' + jpegData.constructor.name + ' value : ' + jpegData);
|
||||
media.getPublicDirectory(mediaLibrary.DirectoryType.DIR_IMAGE, (err, rp) => {
|
||||
if (rp != undefined) {
|
||||
Log.showInfo(TAG, `getPublicDirectory successfull:${JSON.stringify(rp)}`);
|
||||
Log.showInfo(TAG, `getPublicDirectory successful:${JSON.stringify(rp)}`);
|
||||
media.createAsset(mediaLibrary.MediaType.IMAGE, this.imageFilename, rp + SCREEN_SHOT_PATH, (createAssetErr, fileAsset) => {
|
||||
if (fileAsset != undefined) {
|
||||
Log.showInfo(TAG, `createAsset successfull:${JSON.stringify(fileAsset.uri)}`);
|
||||
fileAsset.open('Rw').then((fd) => {
|
||||
Log.showInfo(TAG, `createAsset successful:${JSON.stringify(fileAsset.uri)}`);
|
||||
fileAsset.open('rw').then((fd) => {
|
||||
if (fd == undefined) {
|
||||
Log.showInfo(TAG, `fileAsset open fail`);
|
||||
Log.showInfo(TAG, 'fileAsset open fail');
|
||||
return;
|
||||
}
|
||||
Log.showInfo(TAG, `open successful, fd = ${JSON.stringify(fd)}`);
|
||||
FileIo.write(fd, jpegData).then(num => {
|
||||
Log.showInfo(TAG, `FileIo write, num = ${JSON.stringify(num)}`);
|
||||
fileAsset.close(fd).then(() => {
|
||||
Log.showInfo(TAG, `FileIo close,successfull`);
|
||||
Log.showInfo(TAG, 'FileIo close,successful');
|
||||
})
|
||||
});
|
||||
});
|
||||
} else {
|
||||
Log.showInfo(TAG, `createAsset fail`);
|
||||
Log.showInfo(TAG, 'createAsset fail');
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Log.showInfo(TAG, `getPublicDirectory fail`);
|
||||
Log.showInfo(TAG, 'getPublicDirectory fail');
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
showWindow() {
|
||||
Log.showInfo(TAG, `showWindow`);
|
||||
Log.showInfo(TAG, 'showWindow');
|
||||
WindowMar.find(Constants.WIN_NAME).then((win) => {
|
||||
win.show(() => {
|
||||
Log.showInfo(TAG, `window show`);
|
||||
Log.showInfo(TAG, 'window show');
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
dismiss(): void {
|
||||
dismiss(): void {
|
||||
Log.showInfo(TAG, 'dismiss');
|
||||
//close ability
|
||||
globalThis.shotScreenContext.terminateSelf((err, data) => {
|
||||
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`)
|
||||
Log.showInfo(TAG, `terminateSelf finish err:${err} data:${JSON.stringify(data)}`);
|
||||
WindowMar.find(Constants.WIN_NAME).then((win) => {
|
||||
win.destroy(() => {
|
||||
Log.showInfo(TAG, `destroy the window finish`);
|
||||
Log.showInfo(TAG, 'destroy the window finish');
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -116,10 +117,10 @@ export class ScreenShotModel {
|
||||
|
||||
openAbility(wantData) {
|
||||
Log.showInfo(TAG, `openAbility want:${JSON.stringify(wantData)}`);
|
||||
globalThis.shotScreenContext.startAbility(wantData)
|
||||
globalThis.shotScreenContext.startAbility(wantData);
|
||||
}
|
||||
}
|
||||
|
||||
let screenShotModel = new ScreenShotModel();
|
||||
|
||||
export default screenShotModel as ScreenShotModel
|
||||
export default screenShotModel as ScreenShotModel;
|
@ -14,9 +14,12 @@
|
||||
*/
|
||||
|
||||
import AbilityStage from '@ohos.app.ability.AbilityStage';
|
||||
import Log from '../../../../../../common/src/main/ets/default/Log';
|
||||
|
||||
const TAG = 'MyAbilityStage';
|
||||
|
||||
export default class MyAbilityStage extends AbilityStage {
|
||||
onCreate(): void {
|
||||
console.log('MyAbilityStage onCreate is called');
|
||||
Log.showInfo(TAG, 'MyAbilityStage onCreate is called');
|
||||
}
|
||||
}
|
@ -20,40 +20,43 @@ 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';
|
||||
|
||||
const TAG = "ScreenShot-ScreenShotServiceAbility";
|
||||
const INDEX_PAGE = "pages/index";
|
||||
const TAG = 'ScreenShot-ScreenShotServiceAbility';
|
||||
const INDEX_PAGE = 'pages/index';
|
||||
const ZOOM_RATIO = 0.4;
|
||||
const SHOT_WINDOW_TYPE = 2111;
|
||||
const WINDOW_Y = 300;
|
||||
|
||||
class ServiceExtAbility extends ServiceExtensionAbility {
|
||||
onCreate(want) {
|
||||
Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
|
||||
globalThis.shotScreenContext = this.context;
|
||||
windowManager.create(this.context, Constants.WIN_NAME, SHOT_WINDOW_TYPE).then((win) => {
|
||||
Log.showInfo(TAG, "create window finish!")
|
||||
win.moveTo(0, WINDOW_Y).then(() => {
|
||||
Log.showInfo(TAG, " window moveTo finish")
|
||||
display.getDefaultDisplay().then(dis => {
|
||||
Log.showInfo(TAG, " dis.width = " + dis.width + " dis.height = " + dis.height)
|
||||
win.resetSize(dis.width * ZOOM_RATIO, dis.height * ZOOM_RATIO).then(() => {
|
||||
Log.showInfo(TAG, " window reset size finish")
|
||||
win.loadContent(INDEX_PAGE).then(() => {
|
||||
ScreenShotModel.shotScreen();
|
||||
Log.showInfo(TAG, "then begin window loadContent in then! ");
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}, (error) => {
|
||||
Log.showInfo(TAG, " window createFailed, error.code = " + error.code)
|
||||
onCreate(want) {
|
||||
Log.showInfo(TAG, 'api8New onCreate, want:' + want.abilityName);
|
||||
globalThis.shotScreenContext = this.context;
|
||||
const windowConfig = {
|
||||
name: Constants.WIN_NAME,
|
||||
windowType: windowManager.WindowType.TYPE_SCREENSHOT,
|
||||
ctx: this.context,
|
||||
};
|
||||
windowManager.createWindow(windowConfig).then((win) => {
|
||||
Log.showInfo(TAG, 'create window finish');
|
||||
win.moveWindowTo(0, WINDOW_Y).then(() => {
|
||||
Log.showInfo(TAG, 'window move finish');
|
||||
const dis = display.getDefaultDisplaySync();
|
||||
Log.showInfo(TAG, 'dis.width = ' + dis.width + ' dis.height = ' + dis.height);
|
||||
win.resize(dis.width * ZOOM_RATIO, dis.height * ZOOM_RATIO).then(() => {
|
||||
Log.showInfo(TAG, 'window reset size finish');
|
||||
win.setUIContent(INDEX_PAGE).then(() => {
|
||||
ScreenShotModel.shotScreen();
|
||||
Log.showInfo(TAG, 'then begin window loadContent in then! ');
|
||||
})
|
||||
})
|
||||
Log.showInfo(TAG, " after window create")
|
||||
}
|
||||
})
|
||||
}, (error) => {
|
||||
Log.showInfo(TAG, 'window createFailed, error.code = ' + error.code);
|
||||
})
|
||||
Log.showInfo(TAG, 'after window create');
|
||||
}
|
||||
|
||||
onDestroy() {
|
||||
Log.showInfo(TAG, `onDestroy`);
|
||||
}
|
||||
onDestroy() {
|
||||
Log.showInfo(TAG, 'onDestroy');
|
||||
}
|
||||
}
|
||||
|
||||
export default ServiceExtAbility
|
||||
export default ServiceExtAbility;
|
@ -14,15 +14,13 @@
|
||||
*/
|
||||
|
||||
export default class Constants {
|
||||
|
||||
//layout params - Pic
|
||||
static FULL_CONTAINER_WIDTH = '100%'
|
||||
static FULL_CONTAINER_HEIGHT = '100%'
|
||||
static FULL_CONTAINER_WIDTH = '100%';
|
||||
static FULL_CONTAINER_HEIGHT = '100%';
|
||||
|
||||
static BUTTON_WIDTH = 150
|
||||
static BUTTON_HEIGHT = 80
|
||||
static BUTTON_WIDTH = 150;
|
||||
static BUTTON_HEIGHT = 80;
|
||||
|
||||
//The interval
|
||||
static interval = 5000
|
||||
|
||||
static interval = 5000;
|
||||
}
|
@ -21,8 +21,8 @@ const TAG = 'ScreenShot-Index';
|
||||
@Entry
|
||||
@Component
|
||||
struct Index {
|
||||
@StorageLink("captureImage") captureImage: any = "";
|
||||
@StorageLink("imageFilename") imageFilename: string = "";
|
||||
@StorageLink('captureImage') captureImage: string = '';
|
||||
@StorageLink('imageFilename') imageFilename: string = '';
|
||||
|
||||
build() {
|
||||
Stack({ alignContent: Alignment.Bottom }) {
|
||||
@ -33,7 +33,7 @@ struct Index {
|
||||
.width(Constants.FULL_CONTAINER_WIDTH)
|
||||
.height(Constants.FULL_CONTAINER_HEIGHT)
|
||||
.onClick(() => {
|
||||
ViewModel.StartPhotosAbility(this.imageFilename)
|
||||
ViewModel.StartPhotosAbility(this.imageFilename);
|
||||
})
|
||||
}
|
||||
.width(Constants.FULL_CONTAINER_WIDTH)
|
||||
@ -41,7 +41,7 @@ struct Index {
|
||||
}
|
||||
|
||||
aboutToAppear() {
|
||||
Log.showInfo(TAG, `aboutToAppear`)
|
||||
Log.showInfo(TAG, 'aboutToAppear');
|
||||
setTimeout(ViewModel.CloseShotScreen, Constants.interval);
|
||||
}
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
{
|
||||
"module": {
|
||||
"name": "phone",
|
||||
"type": "feature",
|
||||
"type": "entry",
|
||||
"srcEntrance": "./ets/Application/AbilityStage.ts",
|
||||
"description": "$string:mainability_description",
|
||||
"mainElement": "com.ohos.screenshot.ServiceExtAbility",
|
||||
|
Loading…
Reference in New Issue
Block a user