1.ArkTs整改4 更改ImageKnife->compress文件夹
Signed-off-by: zhoulisheng1 <zhoulisheng1@huawei.com>
This commit is contained in:
parent
803f4934ae
commit
e7d70fea9d
|
@ -25,13 +25,19 @@ import { ImageKnife } from '../ImageKnife'
|
|||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
||||
|
||||
export class CompressBuilder {
|
||||
private _mTargetDir: string;
|
||||
private _mTargetDir: string = '';
|
||||
private _mLeastCompressSize: number= 100; //KB
|
||||
private _mRenameListener: OnRenameListener;
|
||||
private _mCompressListener: OnCompressListener;
|
||||
private _mCompressionPredicate: CompressionPredicate
|
||||
private _mStreamProviders: Array<CompressAdapter>;
|
||||
private _mFocusAlpha: boolean;
|
||||
private _mRenameListener: OnRenameListener = {reName:()=>{return ''}};
|
||||
private _mCompressListener: OnCompressListener={
|
||||
start:()=> {},
|
||||
onScuccess:(p: PixelMap|null|undefined, path: string)=> {},
|
||||
onError:(s: string)=> {}
|
||||
};
|
||||
private _mCompressionPredicate: CompressionPredicate ={
|
||||
apply:(path: string)=> { return false }
|
||||
}
|
||||
private _mStreamProviders: Array<CompressAdapter> = new Array<CompressAdapter>();
|
||||
private _mFocusAlpha: boolean = false;
|
||||
private _outFilePath: string;
|
||||
constructor() {
|
||||
this._mStreamProviders = new Array();
|
||||
|
@ -109,7 +115,7 @@ export class CompressBuilder {
|
|||
if (!this._mTargetDir) {
|
||||
let context:Record<string,Object> = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>);
|
||||
let path = context.filesDir;
|
||||
var timestamp = (new Date()).valueOf();
|
||||
let timestamp = (new Date()).valueOf();
|
||||
this._outFilePath = path + "/compress/" + timestamp + (Math.random() * 100).toFixed(0) + ".jpg";
|
||||
let result = await this.startAsyncCompress();
|
||||
return result;
|
||||
|
@ -120,15 +126,15 @@ export class CompressBuilder {
|
|||
}
|
||||
|
||||
private async startAsyncCompress():Promise<string> {
|
||||
let compressPromise = new Promise((resolve, reject) => {
|
||||
let compressPromise:Promise<string> = new Promise((resolve, reject) => {
|
||||
|
||||
let compressListener: OnCompressListener = {
|
||||
start() {
|
||||
start:()=>{
|
||||
},
|
||||
onScuccess(p: PixelMap, path: string) {
|
||||
onScuccess:(p: PixelMap|undefined|null, path: string)=> {
|
||||
resolve(path);
|
||||
},
|
||||
onError(s: string) {
|
||||
onError:(s: string)=> {
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -139,7 +145,7 @@ export class CompressBuilder {
|
|||
return;
|
||||
}
|
||||
|
||||
for (var i = 0; i < this._mStreamProviders.length; i++) {
|
||||
for (let i = 0; i < this._mStreamProviders.length; i++) {
|
||||
let element = this._mStreamProviders[i];
|
||||
let isOpenfilter = false;
|
||||
if (this._mCompressionPredicate) {
|
||||
|
@ -172,11 +178,11 @@ export class CompressBuilder {
|
|||
return;
|
||||
}
|
||||
if (this._mRenameListener) {
|
||||
var name = this._mRenameListener.reName();
|
||||
let name = this._mRenameListener.reName();
|
||||
if (this._outFilePath && name) {
|
||||
var start = this._outFilePath.lastIndexOf("/") + 1
|
||||
var end = this._outFilePath.length;
|
||||
var replaceStr = this._outFilePath.substring(start, end);
|
||||
let start = this._outFilePath.lastIndexOf("/") + 1
|
||||
let end = this._outFilePath.length;
|
||||
let replaceStr = this._outFilePath.substring(start, end);
|
||||
this._outFilePath = this._outFilePath.replace(replaceStr, name);
|
||||
}
|
||||
}
|
||||
|
@ -184,7 +190,7 @@ export class CompressBuilder {
|
|||
this._mCompressListener.start();
|
||||
}
|
||||
|
||||
for (var i = 0; i < this._mStreamProviders.length; i++) {
|
||||
for (let i = 0; i < this._mStreamProviders.length; i++) {
|
||||
let element = this._mStreamProviders[i];
|
||||
let isOpenfilter = false;
|
||||
if (this._mCompressionPredicate) {
|
||||
|
@ -203,7 +209,7 @@ export class CompressBuilder {
|
|||
|
||||
private getImageCacheFile() {
|
||||
let context:Record<string,Object> = (ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>)
|
||||
var timestamp = (new Date()).valueOf();
|
||||
let timestamp = (new Date()).valueOf();
|
||||
this._outFilePath = context.filesDir + "/compress/" + timestamp + (Math.random() * 100).toFixed(0) + ".jpg";
|
||||
this.startCompress();
|
||||
}
|
||||
|
|
|
@ -13,14 +13,15 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {OnCompressListener} from '../compress/listener/OnCompressListener'
|
||||
import {CompressionPredicate} from '../compress/listener/CompressionPredicate'
|
||||
import {CompressAdapter} from "../compress/provider/CompressAdapter"
|
||||
import {DataStringPathProvider} from '../compress/provider/DataStringPathProvider'
|
||||
import {RecourseProvider} from '../compress/provider/RecourseProvider'
|
||||
import {FileUtils} from '../../cache/FileUtils'
|
||||
import { OnCompressListener } from '../compress/listener/OnCompressListener'
|
||||
import { CompressionPredicate } from '../compress/listener/CompressionPredicate'
|
||||
import { CompressAdapter } from "../compress/provider/CompressAdapter"
|
||||
import { DataStringPathProvider } from '../compress/provider/DataStringPathProvider'
|
||||
import { RecourseProvider } from '../compress/provider/RecourseProvider'
|
||||
import { FileUtils } from '../../cache/FileUtils'
|
||||
import image from "@ohos.multimedia.image"
|
||||
import fileio from '@ohos.fileio';
|
||||
import fs from '@ohos.file.fs';
|
||||
import { BusinessError } from '@ohos.base'
|
||||
|
||||
export class Engine {
|
||||
private mFocusAlpha: boolean;
|
||||
|
@ -48,10 +49,10 @@ export class Engine {
|
|||
srcWidth = srcWidth % 2 == 1 ? srcWidth + 1 : srcWidth;
|
||||
srcHeight = srcHeight % 2 == 1 ? srcHeight + 1 : srcHeight;
|
||||
|
||||
var longSide = Math.max(srcWidth, srcHeight);
|
||||
var shortSide = Math.min(srcWidth, srcHeight);
|
||||
let longSide = Math.max(srcWidth, srcHeight);
|
||||
let shortSide = Math.min(srcWidth, srcHeight);
|
||||
|
||||
var scale = shortSide / longSide;
|
||||
let scale = shortSide / longSide;
|
||||
if (scale <= 1 && scale > 0.5625) {
|
||||
if (longSide < 1664) {
|
||||
return 1;
|
||||
|
@ -72,7 +73,7 @@ export class Engine {
|
|||
compress() {
|
||||
if (this.mCompressAdapter instanceof DataStringPathProvider) {
|
||||
// file
|
||||
this.mCompressAdapter.openInternal((buffer) => {
|
||||
this.mCompressAdapter.openInternal({ compressDataListener: (buffer: ArrayBuffer) => {
|
||||
if (!buffer) {
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onError("file read fail,and date is empty")
|
||||
|
@ -83,36 +84,36 @@ export class Engine {
|
|||
return;
|
||||
}
|
||||
|
||||
var imageResource = image.createImageSource(buffer as any);
|
||||
let imageResource: image.ImageSource = image.createImageSource(buffer as any);
|
||||
imageResource.getImageInfo()
|
||||
.then(info => {
|
||||
var height = info.size.height;
|
||||
var width = info.size.width;
|
||||
var computeSize = this.computeSize(width, height);
|
||||
console.info("Engine compress computeSize:" + computeSize);
|
||||
let options = {
|
||||
editable: true,
|
||||
sampleSize: computeSize,
|
||||
desiredPixelFormat: image.PixelMapFormat.RGB_565,
|
||||
}
|
||||
imageResource.createPixelMap(options)
|
||||
.then(bitmap => {
|
||||
let height = info.size.height;
|
||||
let width = info.size.width;
|
||||
let computeSize = this.computeSize(width, height);
|
||||
console.info("Engine compress computeSize:" + computeSize);
|
||||
let options: image.DecodingOptions = {
|
||||
editable: true,
|
||||
sampleSize: computeSize,
|
||||
desiredPixelFormat: image.PixelMapFormat.RGB_565,
|
||||
}
|
||||
imageResource.createPixelMap(options)
|
||||
.then(bitmap => {
|
||||
|
||||
})
|
||||
.catch((error: BusinessError) => {
|
||||
this.mCompressListener.onError("ptah createPixelMap fail,because error:" + JSON.stringify(error as BusinessError))
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
.catch(error => {
|
||||
this.mCompressListener.onError("ptah createPixelMap fail,because error:" + JSON.stringify(error))
|
||||
.catch((error: BusinessError) => {
|
||||
this.mCompressListener.onError("ptah getImageInfo fail,because error:" + JSON.stringify(error as BusinessError))
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
.catch(error => {
|
||||
this.mCompressListener.onError("ptah getImageInfo fail,because error:" + JSON.stringify(error))
|
||||
})
|
||||
|
||||
})
|
||||
} })
|
||||
} else if (this.mCompressAdapter instanceof RecourseProvider) {
|
||||
// resource
|
||||
this.mCompressAdapter.openInternal((buffer) => {
|
||||
this.mCompressAdapter.openInternal({ compressDataListener: (buffer: ArrayBuffer) => {
|
||||
if (!buffer) {
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onError("resource read fail,and date is empty")
|
||||
|
@ -122,86 +123,78 @@ export class Engine {
|
|||
if (!this.checkNeedCompress(buffer)) {
|
||||
return;
|
||||
}
|
||||
var imageResource = image.createImageSource(buffer as any);
|
||||
let imageResource: image.ImageSource = image.createImageSource(buffer);
|
||||
imageResource.getImageInfo()
|
||||
.then(info => {
|
||||
var height = info.size.height;
|
||||
var width = info.size.width;
|
||||
var computeSize = this.computeSize(width, height);
|
||||
let packer = {
|
||||
format: ["image/jpeg"],
|
||||
quality: computeSize,
|
||||
}
|
||||
var imagePacker = image.createImagePacker();
|
||||
imagePacker.packing(imageResource, packer as any, (err,compressBuffer)=>{
|
||||
if(err){
|
||||
this.mCompressListener.onError("resource packing fail,because error:" + err);
|
||||
let height = info.size.height;
|
||||
let width = info.size.width;
|
||||
let computeSize = this.computeSize(width, height);
|
||||
let packer: image.PackingOption = {
|
||||
format: "image/jpeg",
|
||||
quality: computeSize,
|
||||
}
|
||||
let imagePacker: image.ImagePacker = image.createImagePacker();
|
||||
imagePacker.packing(imageResource, packer, (err, compressBuffer) => {
|
||||
if (err) {
|
||||
this.mCompressListener.onError("resource packing fail,because error:" + err);
|
||||
}
|
||||
|
||||
console.log("compressBuffer is null ="+ (compressBuffer==null));
|
||||
console.log("compressBuffer is undefined ="+ (compressBuffer == undefined) );
|
||||
let dirPath = this.mPath.substring(0, this.mPath.lastIndexOf("/") + 1);
|
||||
var isDirectory = this.checkDirExist(dirPath);
|
||||
if (isDirectory) {
|
||||
this.saveFile(this.mPath, compressBuffer);
|
||||
this.handResult(compressBuffer, this.mPath);
|
||||
} else {
|
||||
fileio.mkdir(dirPath)
|
||||
.then(() => {
|
||||
console.log("compressBuffer is null =" + (compressBuffer == null));
|
||||
console.log("compressBuffer is undefined =" + (compressBuffer == undefined));
|
||||
let dirPath = this.mPath.substring(0, this.mPath.lastIndexOf("/") + 1);
|
||||
let isDirectory = this.checkDirExist(dirPath);
|
||||
if (isDirectory) {
|
||||
this.saveFile(this.mPath, compressBuffer);
|
||||
this.handResult(compressBuffer, this.mPath);
|
||||
});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
fs.mkdir(dirPath)
|
||||
.then(() => {
|
||||
this.saveFile(this.mPath, compressBuffer);
|
||||
this.handResult(compressBuffer, this.mPath);
|
||||
});
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
})
|
||||
})
|
||||
|
||||
} })
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private handResult(buffer: ArrayBuffer, path: string) {
|
||||
var imageRes = image.createImageSource(buffer as any);
|
||||
let a={
|
||||
let imageRes:image.ImageSource = image.createImageSource(buffer);
|
||||
let a:image.DecodingOptions = {
|
||||
editable: true,
|
||||
}
|
||||
imageRes.createPixelMap(a)
|
||||
.then(bitmap => {
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onScuccess(bitmap, path);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onError("buffer generated pixelMap fail,because error:" + JSON.stringify(error))
|
||||
}
|
||||
})
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onScuccess(bitmap, path);
|
||||
}
|
||||
})
|
||||
.catch((error:BusinessError) => {
|
||||
if (this.mCompressListener) {
|
||||
this.mCompressListener.onError("buffer generated pixelMap fail,because error:" + JSON.stringify(error as BusinessError))
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private checkNeedCompress(buf: ArrayBuffer): boolean{
|
||||
private checkNeedCompress(buf: ArrayBuffer): boolean {
|
||||
if (!buf) {
|
||||
return false;
|
||||
}
|
||||
var length = buf.byteLength / 1024;
|
||||
let length = buf.byteLength / 1024;
|
||||
return length > this._mLeastCompressSize;
|
||||
}
|
||||
|
||||
private saveFile(path: string, content: ArrayBuffer) {
|
||||
FileUtils.getInstance().writeFile(path,content);
|
||||
FileUtils.getInstance().writeFile(path, content);
|
||||
}
|
||||
|
||||
private checkDirExist(dirPath: string): boolean{
|
||||
var isExist;
|
||||
try {
|
||||
fileio.accessSync(dirPath, 0)
|
||||
isExist = true;
|
||||
} catch (e) {
|
||||
//不符合条件则进入
|
||||
isExist = false;
|
||||
}
|
||||
private checkDirExist(dirPath: string): boolean {
|
||||
let isExist:boolean = fs.accessSync(dirPath);
|
||||
return isExist;
|
||||
}
|
||||
}
|
|
@ -14,5 +14,5 @@
|
|||
*/
|
||||
|
||||
export interface CompressDataListener<T> {
|
||||
(t: T);
|
||||
compressDataListener:(t: T)=>void;
|
||||
}
|
|
@ -17,5 +17,5 @@
|
|||
* filter out unsupported
|
||||
*/
|
||||
export interface CompressionPredicate {
|
||||
apply(path: string): boolean;
|
||||
apply:(path: string)=>boolean;
|
||||
}
|
|
@ -19,13 +19,13 @@ export interface OnCompressListener {
|
|||
/**
|
||||
* compress start
|
||||
*/
|
||||
start();
|
||||
start:()=>void;
|
||||
/**
|
||||
* compress success
|
||||
*/
|
||||
onScuccess(p: PixelMap, path: string);
|
||||
onScuccess:(p: PixelMap|undefined|null, path: string)=>void;
|
||||
/**
|
||||
* compress fail
|
||||
*/
|
||||
onError(s: string);
|
||||
onError:(s: string)=>void;
|
||||
}
|
|
@ -17,5 +17,5 @@
|
|||
* rename listener
|
||||
*/
|
||||
export interface OnRenameListener {
|
||||
reName():string;
|
||||
reName:()=>string;
|
||||
}
|
|
@ -35,7 +35,7 @@ export abstract class CompressAdapter implements CompressProvider {
|
|||
|
||||
abstract getRecoursePath(): string;
|
||||
|
||||
abstract getPixelMapFormat(): PixelMapFormat;
|
||||
abstract getPixelMapFormat(): PixelMapFormat | undefined;
|
||||
|
||||
getFormat(s: string): PixelMapFormat | undefined{
|
||||
if (!s) {
|
||||
|
|
|
@ -31,14 +31,14 @@ export class DataStringPathProvider extends CompressAdapter {
|
|||
if (!this.mPath) {
|
||||
throw new Error('DataStringPathProvider error path is empty');
|
||||
}
|
||||
var buffer = FileUtils.getInstance().readFilePic(this.mPath);
|
||||
let buffer = FileUtils.getInstance().readFilePic(this.mPath);
|
||||
this.mData = buffer;
|
||||
if (callback) {
|
||||
callback(buffer);
|
||||
callback.compressDataListener(buffer);
|
||||
}
|
||||
}
|
||||
|
||||
getPixelMapFormat(): PixelMapFormat{
|
||||
getPixelMapFormat(): PixelMapFormat|undefined{
|
||||
if (!this.mPath) {
|
||||
return PixelMapFormat.NONE;
|
||||
}
|
||||
|
|
|
@ -13,29 +13,30 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import {CompressAdapter, PixelMapFormat} from "../provider/CompressAdapter"
|
||||
import {CompressDataListener} from "../listener/CompressDataListener"
|
||||
import {FileTypeUtil} from '../../../imageknife/utils/FileTypeUtil'
|
||||
import { CompressAdapter, PixelMapFormat } from "../provider/CompressAdapter"
|
||||
import { CompressDataListener } from "../listener/CompressDataListener"
|
||||
import { FileTypeUtil } from '../../../imageknife/utils/FileTypeUtil'
|
||||
import { ImageKnifeGlobal } from '../../ImageKnifeGlobal';
|
||||
import resourceManager from '@ohos.resourceManager';
|
||||
import { BusinessError } from '@ohos.base'
|
||||
|
||||
export class RecourseProvider extends CompressAdapter {
|
||||
private static CHARS: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||
private static DEFAULT_RESOURCE_PATH: string= "Resource"
|
||||
private static DEFAULT_RESOURCE_PATH: string = "Resource"
|
||||
private _mLookup: Uint8Array = new Uint8Array(256);
|
||||
private _mResourceData: Resource;
|
||||
private _mPixelMapHeader: string;
|
||||
private _mResourceData?: Resource = undefined;
|
||||
private _mPixelMapHeader: string = '';
|
||||
|
||||
constructor(s: Resource) {
|
||||
super()
|
||||
this._mResourceData = s;
|
||||
this.mPath = RecourseProvider.DEFAULT_RESOURCE_PATH;
|
||||
for (var index = 0; index < RecourseProvider.CHARS.length; index++) {
|
||||
for (let index = 0; index < RecourseProvider.CHARS.length; index++) {
|
||||
this._mLookup[RecourseProvider.CHARS.charCodeAt(index)] = index;
|
||||
}
|
||||
}
|
||||
|
||||
getRecoursePath(): string{
|
||||
getRecoursePath(): string {
|
||||
return this.mPath;
|
||||
}
|
||||
|
||||
|
@ -44,21 +45,22 @@ export class RecourseProvider extends CompressAdapter {
|
|||
if (!this._mResourceData) {
|
||||
throw Error("compress resource is empty");
|
||||
}
|
||||
((ImageKnifeGlobal.getInstance().getHapContext() as Record<string,Object>).resourceManager as resourceManager.ResourceManager)
|
||||
((ImageKnifeGlobal.getInstance()
|
||||
.getHapContext() as Record<string, Object>).resourceManager as resourceManager.ResourceManager)
|
||||
.getMediaContent(this._mResourceData.id)
|
||||
.then(data => {
|
||||
let buffer = this.uint8ArrayToBuffer(data);
|
||||
let fileTypeUtil = new FileTypeUtil()
|
||||
this._mPixelMapHeader = fileTypeUtil.getFileType(buffer);
|
||||
callback(buffer);
|
||||
callback.compressDataListener(buffer);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log("RecourseProvider openInternal err" + JSON.stringify(err));
|
||||
.catch((err: BusinessError) => {
|
||||
console.log("RecourseProvider openInternal err" + JSON.stringify(err as BusinessError));
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
getPixelMapFormat(): PixelMapFormat{
|
||||
getPixelMapFormat(): PixelMapFormat|undefined{
|
||||
if (!this._mPixelMapHeader) {
|
||||
return PixelMapFormat.NONE;
|
||||
}
|
||||
|
@ -68,14 +70,14 @@ export class RecourseProvider extends CompressAdapter {
|
|||
* data decode
|
||||
*/
|
||||
decode(base64: string, callback: CompressDataListener<ArrayBuffer>) {
|
||||
let bufferLength = base64.length,
|
||||
len = base64.length,
|
||||
i,
|
||||
p = 0,
|
||||
encoded1,
|
||||
encoded2,
|
||||
encoded3,
|
||||
encoded4;
|
||||
let bufferLength: number = base64.length;
|
||||
let len: number = base64.length;
|
||||
let i: number = 0;
|
||||
let p: number = 0;
|
||||
let encoded1: number = 0;
|
||||
let encoded2: number = 0;
|
||||
let encoded3: number = 0;
|
||||
let encoded4: number = 0;
|
||||
|
||||
if (base64[base64.length - 1] === '=') {
|
||||
bufferLength--;
|
||||
|
@ -100,7 +102,7 @@ export class RecourseProvider extends CompressAdapter {
|
|||
}
|
||||
this.mData = arraybuffer;
|
||||
if (callback) {
|
||||
callback(arraybuffer);
|
||||
callback.compressDataListener(arraybuffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue