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 "snapshot_utils.h"
26
27 using namespace OHOS;
28 using namespace OHOS::Media;
29 using namespace OHOS::Rosen;
30
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33 CmdArgments cmdArgments;
34 cmdArgments.fileName = "";
35
36 if (!SnapShotUtils::ProcessArgs(argc, argv, cmdArgments)) {
37 return 0;
38 }
39
40 auto display = DisplayManager::GetInstance().GetDisplayById(cmdArgments.displayId);
41 if (display == nullptr) {
42 std::cout << "error: GetDisplayById " << cmdArgments.displayId << " error!" << std::endl;
43 return -1;
44 }
45
46 std::cout << "process: display " << cmdArgments.displayId <<
47 ": width " << display->GetWidth() << ", height " << display->GetHeight() << std::endl;
48
49 // get PixelMap from DisplayManager API
50 std::shared_ptr<OHOS::Media::PixelMap> pixelMap = nullptr;
51 if (!cmdArgments.isWidthSet && !cmdArgments.isHeightSet) {
52 pixelMap = DisplayManager::GetInstance().GetScreenshot(cmdArgments.displayId); // default width & height
53 } else {
54 if (!cmdArgments.isWidthSet) {
55 cmdArgments.width = display->GetWidth();
56 std::cout << "process: reset to display's width " << cmdArgments.width << std::endl;
57 }
58 if (!cmdArgments.isHeightSet) {
59 cmdArgments.height = display->GetHeight();
60 std::cout << "process: reset to display's height " << cmdArgments.height << std::endl;
61 }
62 if (!SnapShotUtils::CheckWidthAndHeightValid(cmdArgments.width, cmdArgments.height)) {
63 std::cout << "error: width " << cmdArgments.width << " height " <<
64 cmdArgments.height << " invalid!" << std::endl;
65 return -1;
66 }
67 const Media::Rect rect = {0, 0, display->GetWidth(), display->GetHeight()};
68 const Media::Size size = {cmdArgments.width, cmdArgments.height};
69 constexpr int rotation = 0;
70 pixelMap = DisplayManager::GetInstance().GetScreenshot(cmdArgments.displayId, rect, size, rotation);
71 }
72
73 bool ret = false;
74 if (pixelMap != nullptr) {
75 ret = SnapShotUtils::WriteToJpegWithPixelMap(cmdArgments.fileName, *pixelMap);
76 }
77 if (!ret) {
78 std::cout << "\nerror: snapshot display " << cmdArgments.displayId <<
79 ", write to " << cmdArgments.fileName.c_str() << " as jpeg failed!" << std::endl;
80 return -1;
81 }
82
83 std::cout << "\nsuccess: snapshot display " << cmdArgments.displayId << " , write to " <<
84 cmdArgments.fileName.c_str() << " as jpeg, width " << pixelMap->GetWidth() <<
85 ", height " << pixelMap->GetHeight() << std::endl;
86 return 0;
87 }