1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <cstdio>
17 #include <image_type.h>
18 #include <iosfwd>
19 #include <iostream>
20 #include <memory>
21 #include <ostream>
22 #include <refbase.h>
23
24 #include "display_manager.h"
25 #include "parameters.h"
26 #include "snapshot_utils.h"
27
28 using namespace OHOS;
29 using namespace OHOS::Media;
30 using namespace OHOS::Rosen;
31 using OHOS::system::GetParameter;
32
33 // developer mode
34 static const std::string DEVELOPER_MODE_STATE_ON_DEFAULT = "false";
35 static const std::string DEVELOPER_MODE_PARAMETER = "const.security.developermode.state";
36 static const std::string IS_DEVELOPER_MODE = GetParameter(DEVELOPER_MODE_PARAMETER, DEVELOPER_MODE_STATE_ON_DEFAULT);
37
38 static bool GetScreenshotByCmdArgments(CmdArgments& cmdArgments, sptr<Display> display,
39 std::shared_ptr<OHOS::Media::PixelMap>& pixelMap);
40
main(int argc,char * argv[])41 int main(int argc, char *argv[])
42 {
43 CmdArgments cmdArgments;
44 cmdArgments.fileName = "";
45
46 if (!SnapShotUtils::ProcessArgs(argc, argv, cmdArgments)) {
47 _exit(-1);
48 }
49
50 if (DEVELOPER_MODE_STATE_ON_DEFAULT == IS_DEVELOPER_MODE) {
51 std::cout << "current mode is not developer mode, just return." << std::endl;
52 _exit(-1);
53 }
54
55 auto display = DisplayManager::GetInstance().GetDisplayById(cmdArgments.displayId);
56 if (display == nullptr) {
57 std::cout << "error: GetDisplayById " << cmdArgments.displayId << " error!" << std::endl;
58 _exit(-1);
59 }
60
61 std::cout << "process: display " << cmdArgments.displayId <<
62 ": width " << display->GetWidth() << ", height " << display->GetHeight() << std::endl;
63
64 // get PixelMap from DisplayManager API
65 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = nullptr;
66 if (!GetScreenshotByCmdArgments(cmdArgments, display, pixelMap)) {
67 _exit(-1);
68 }
69
70 bool ret = false;
71 if (pixelMap != nullptr) {
72 ret = SnapShotUtils::WriteToJpegWithPixelMap(cmdArgments.fileName, *pixelMap);
73 }
74 if (!ret) {
75 std::cout << "\nerror: snapshot display " << cmdArgments.displayId <<
76 ", write to " << cmdArgments.fileName << " as jpeg failed!" << std::endl;
77 _exit(-1);
78 }
79
80 std::cout << "\nsuccess: snapshot display " << cmdArgments.displayId << " , write to " <<
81 cmdArgments.fileName << " as jpeg, width " << pixelMap->GetWidth() <<
82 ", height " << pixelMap->GetHeight() << std::endl;
83 _exit(0);
84 }
85
GetScreenshotByCmdArgments(CmdArgments & cmdArgments,sptr<Display> display,std::shared_ptr<OHOS::Media::PixelMap> & pixelMap)86 static bool GetScreenshotByCmdArgments(CmdArgments& cmdArgments, sptr<Display> display,
87 std::shared_ptr<OHOS::Media::PixelMap>& pixelMap)
88 {
89 DmErrorCode errorCode;
90 if (!cmdArgments.isWidthSet && !cmdArgments.isHeightSet) {
91 // default width & height
92 pixelMap = DisplayManager::GetInstance().GetScreenshot(cmdArgments.displayId, &errorCode, false);
93 } else {
94 if (!cmdArgments.isWidthSet) {
95 cmdArgments.width = display->GetWidth();
96 std::cout << "process: reset to display's width " << cmdArgments.width << std::endl;
97 }
98 if (!cmdArgments.isHeightSet) {
99 cmdArgments.height = display->GetHeight();
100 std::cout << "process: reset to display's height " << cmdArgments.height << std::endl;
101 }
102 if (!SnapShotUtils::CheckWidthAndHeightValid(cmdArgments.width, cmdArgments.height)) {
103 std::cout << "error: width " << cmdArgments.width << " height " <<
104 cmdArgments.height << " invalid!" << std::endl;
105 return false;
106 }
107 SnapShotConfig snapConfig;
108 snapConfig.displayId_ = cmdArgments.displayId;
109 snapConfig.imageRect_ = { 0, 0, display->GetWidth(), display->GetHeight() };
110 snapConfig.imageSize_ = { cmdArgments.width, cmdArgments.height };
111 snapConfig.rotation_ = 0;
112 pixelMap = DisplayManager::GetInstance().GetScreenshotwithConfig(snapConfig, &errorCode, false);
113 }
114 return true;
115 }