1
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"minSdkVersion": "21"
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import ViewGroup from 'android.view.ViewGroup';
|
||||
import Bitmap from 'android.graphics.Bitmap';
|
||||
import View from 'android.view.View';
|
||||
import FileOutputStream from 'java.io.FileOutputStream';
|
||||
import File from 'java.io.File';
|
||||
import Canvas from 'android.graphics.Canvas';
|
||||
function savePhotoTocache(img : Bitmap) : string {
|
||||
let context = UTSAndroid.getAppContext()!
|
||||
let filepath = context.getExternalCacheDir()?.getPath() ?? ""
|
||||
let fileid = Math.random().toString(16).substring(2, 11);
|
||||
let file = new File(filepath, fileid + '.jpg');
|
||||
let fos = new FileOutputStream(file)
|
||||
img.compress(Bitmap.CompressFormat.JPEG, 100, fos);
|
||||
return file.getPath()
|
||||
}
|
||||
function getScreenImage() {
|
||||
let decorView : ViewGroup = UTSAndroid.getUniActivity()!.window.decorView as ViewGroup
|
||||
let rootview : View = decorView.getRootView()
|
||||
// rootview.setDrawingCacheEnabled(true)
|
||||
// let bitmap = Bitmap.createBitmap(rootview.getDrawingCache())
|
||||
// rootview.setDrawingCacheEnabled(false)
|
||||
|
||||
let bitmap = Bitmap.createBitmap(rootview.width, rootview.height, Bitmap.Config.ARGB_8888)
|
||||
let canvas = new Canvas(bitmap)
|
||||
rootview.draw(canvas)
|
||||
|
||||
|
||||
return savePhotoTocache(bitmap);
|
||||
}
|
||||
/**
|
||||
* 获取窗口截图
|
||||
*/
|
||||
export function getRootShotImage(callback : (str : string) => void) {
|
||||
callback(getScreenImage())
|
||||
}
|
||||
/**
|
||||
* 获取指定元素截图
|
||||
*/
|
||||
export function getElementShotImage(ele:UniElement|null, callback : (str : string) => void) {
|
||||
if(ele == null) {
|
||||
callback("")
|
||||
return;
|
||||
}
|
||||
let view = ele.getAndroidView() as View|null;
|
||||
if(view == null) {
|
||||
callback("")
|
||||
return;
|
||||
}
|
||||
let rootview = view as View;
|
||||
// rootview.setDrawingCacheEnabled(true)
|
||||
// let bitmap = Bitmap.createBitmap(rootview.getDrawingCache())
|
||||
// rootview.setDrawingCacheEnabled(false)
|
||||
let bitmap = Bitmap.createBitmap(rootview.width, rootview.height, Bitmap.Config.ARGB_8888)
|
||||
let canvas = new Canvas(bitmap)
|
||||
rootview.draw(canvas)
|
||||
|
||||
let imgpath:string = savePhotoTocache(bitmap);
|
||||
callback(imgpath)
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"x_screenshot_s": "./lib/x_screenshot_s.har"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
import { x_getElementShotImage,x_getRootShotImage } from "x_screenshot_s";
|
||||
/**
|
||||
* 获取窗口截图
|
||||
*/
|
||||
export function getRootShotImage(callback : (str : string) => void) {
|
||||
x_getRootShotImage(UTSHarmony.getCurrentWindow()!.getUIContext(),callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定元素截图
|
||||
*/
|
||||
export function getElementShotImage(ele:UniElement|null, callback : (str : string) => void) {
|
||||
x_getElementShotImage(UTSHarmony.getCurrentWindow()!.getUIContext(),ele?.getAttribute("id"),callback)
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归查询
|
||||
*/
|
||||
function iterateElement(homeElement : UniElement) : UniNativeViewElement | null {
|
||||
if ("NATIVE-VIEW" == homeElement.nodeName) {
|
||||
return homeElement as UniNativeViewElement
|
||||
}
|
||||
for (const perChildEle of homeElement.children) {
|
||||
let findEle = iterateElement(perChildEle)
|
||||
if (findEle != null) {
|
||||
return findEle
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
function getPageRootView(id : string, ins : ComponentPublicInstance | null = null) : FrameNode | null {
|
||||
if (ins == null) {
|
||||
const pages = getCurrentPages()
|
||||
if (pages.length > 0) {
|
||||
const page = pages[pages.length - 1]
|
||||
const rootViewElement = page.getElementById(id)
|
||||
if (rootViewElement != null) {
|
||||
/**
|
||||
* 找到了root节点,递归检索目标 native-view
|
||||
*/
|
||||
const nativeViewElement = iterateElement(rootViewElement)
|
||||
|
||||
if (nativeViewElement != null) {
|
||||
return nativeViewElement.getHarmonyFrameNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
/**
|
||||
* 尝试迭代遍历
|
||||
*/
|
||||
if (ins.$el != null) {
|
||||
const nativeViewElement = iterateElement(ins.$el as UniElement)
|
||||
if (nativeViewElement != null) {
|
||||
|
||||
return nativeViewElement.getHarmonyFrameNode()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null
|
||||
}
|
||||
Binary file not shown.
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>NSPrivacyAccessedAPITypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyCollectedDataTypes</key>
|
||||
<array/>
|
||||
<key>NSPrivacyTracking</key>
|
||||
<false/>
|
||||
<key>NSPrivacyTrackingDomains</key>
|
||||
<array/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"deploymentTarget": "10"
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import { UIButton, UIControl, UIImage, UIView, UIImageView, UIImageWriteToSavedPhotosAlbum, UIGraphicsImageRenderer } from "UIKit";
|
||||
import { URL, FileManager, NSAttributedString } from 'Foundation';
|
||||
function savePhotoTocache(image : UIImage) : string {
|
||||
let fileid = Math.random().toString(16).substring(2, 11);
|
||||
let userDir = FileManager.default.urls(for = FileManager.SearchPathDirectory.cachesDirectory, in = FileManager.SearchPathDomainMask.userDomainMask).first!
|
||||
let filename = fileid + '.jpg';
|
||||
let destinationURL = userDir.appendingPathComponent(filename)
|
||||
let imageData = image.jpegData(compressionQuality = 1);
|
||||
let real = UTSiOS.try(imageData!.write(to = destinationURL), "?");
|
||||
if (real == null) return ""
|
||||
return destinationURL.path
|
||||
}
|
||||
function getScreenImage(callback : (str : string) => void) {
|
||||
if (UTSiOS.available("iOS 10.0, *")) {
|
||||
let parent = UTSiOS.getCurrentViewController() as UIViewController;
|
||||
let rootView = parent.view as UIView
|
||||
let renderer = new UIGraphicsImageRenderer(size = rootView.bounds.size);
|
||||
renderer.image(actions = (context : UIGraphicsImageRendererContext) => {
|
||||
rootView.drawHierarchy(in = rootView.bounds, afterScreenUpdates = true)
|
||||
let image = context.currentImage
|
||||
callback(savePhotoTocache(image))
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* 获取窗口截图
|
||||
*/
|
||||
export function getRootShotImage(callback : (str : string) => void) {
|
||||
getScreenImage((str : string) => {
|
||||
callback(str)
|
||||
})
|
||||
}
|
||||
/**
|
||||
* 获取指定元素截图
|
||||
*/
|
||||
export function getElementShotImage(ele:UniElement|null, callback : (str : string) => void) {
|
||||
if (ele == null) {
|
||||
callback("")
|
||||
return;
|
||||
}
|
||||
let view = ele!.getIOSView() as UIView | null;
|
||||
if (view == null) {
|
||||
callback("")
|
||||
return;
|
||||
}
|
||||
let rootView = view! as UIView;
|
||||
if (UTSiOS.available("iOS 10.0, *")) {
|
||||
let renderer = new UIGraphicsImageRenderer(size = rootView.bounds.size);
|
||||
renderer.image(actions = (context : UIGraphicsImageRendererContext) => {
|
||||
rootView.drawHierarchy(in = rootView.bounds, afterScreenUpdates = true)
|
||||
let image = context.currentImage
|
||||
callback(savePhotoTocache(image))
|
||||
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
/**
|
||||
* 获取窗口截图
|
||||
*/
|
||||
export function getRootShotImage(callback : (str : string) => void) {
|
||||
callback("")
|
||||
}
|
||||
/**
|
||||
* 获取指定元素截图
|
||||
*/
|
||||
export function getElementShotImage(ele:UniElement|null, callback : (str : string) => void) {
|
||||
callback("")
|
||||
}
|
||||
Reference in New Issue
Block a user