mirror of
https://gitee.com/openharmony/applications_photos
synced 2025-02-18 14:22:29 +00:00
!461 【master】【OpenHarmony 4.0.10.15】【应用子系统】【标准系统】【必现_5/5】图库照片镜像后大小不一致优化
Merge pull request !461 from liujuan/master
This commit is contained in:
commit
eb2478a3cd
@ -18,6 +18,7 @@ import { BrowserDataFactory, ImageUtil, Log, ScreenManager } from '@ohos/common'
|
||||
import { PixelMapWrapper } from './base/PixelMapWrapper';
|
||||
import { CropAngle } from './crop/CropType';
|
||||
import userFileManager from '@ohos.filemanagement.userFileManager';
|
||||
import image from '@ohos.multimedia.image';
|
||||
|
||||
const TAG: string = 'editor_Loader';
|
||||
|
||||
@ -39,9 +40,24 @@ export class Loader {
|
||||
height: result.get(userFileManager.ImageVideoKey.HEIGHT.toString()) as number
|
||||
};
|
||||
|
||||
isPreview && Loader.getPixelMapPreviewSize(size);
|
||||
let thumbnail = undefined;
|
||||
if (isPreview) {
|
||||
Loader.getPixelMapPreviewSize(size);
|
||||
thumbnail = await result.getThumbnail(size);
|
||||
} else {
|
||||
let fd = undefined;
|
||||
let imageSourceApi: image.ImageSource = undefined;
|
||||
try {
|
||||
fd = await result.open('rw');
|
||||
imageSourceApi = image.createImageSource(fd);
|
||||
thumbnail = await imageSourceApi.createPixelMap();
|
||||
} catch (e) {
|
||||
Log.error(TAG, "loadPixelMapWrapper: sth wrong, " + e);
|
||||
} finally {
|
||||
await result.close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
let thumbnail = await result.getThumbnail(size);
|
||||
let wrapper = new PixelMapWrapper(thumbnail, px2vp(size.width), px2vp(size.height));
|
||||
Log.info(TAG, `Photo: loadPixelMap: size[${JSON.stringify(size)}] wrapper[${JSON.stringify(wrapper)}]`);
|
||||
|
||||
@ -94,4 +110,30 @@ export class Loader {
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
static async getCompressedBitsPerPixel(mediaItem: MediaItem): Promise<number> {
|
||||
return new Promise(async (resolve, reject)=> {
|
||||
let dataImpl = BrowserDataFactory.getFeature(BrowserDataFactory.TYPE_PHOTO);
|
||||
let result = await dataImpl.getDataByUri(mediaItem.uri);
|
||||
if (!result) {
|
||||
Log.error(TAG, 'getCompressedBitsPerPixel: get file asset failed.');
|
||||
reject();
|
||||
}
|
||||
|
||||
let fd = await result.open('rw');
|
||||
let imageSourceApi: image.ImageSource = image.createImageSource(fd);
|
||||
imageSourceApi = image.createImageSource(fd);
|
||||
imageSourceApi.getImageProperty("CompressedBitsPerPixel", (error, data) => {
|
||||
if (error) {
|
||||
Log.error(TAG, 'Failed to get the value of the specified attribute key of the image.');
|
||||
result.close(fd);
|
||||
reject();
|
||||
} else {
|
||||
Log.error(TAG, 'Succeeded in getting the value of the specified attribute key of the image. data is ' + data);
|
||||
result.close(fd);
|
||||
resolve(Number(data));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
@ -32,7 +32,8 @@ import type { Album, FileType } from '@ohos/common/src/main/ets/default/access/U
|
||||
const TAG: string = 'editor_Save';
|
||||
|
||||
export class Save {
|
||||
private static readonly QUALITY_100: number = 100;
|
||||
private static readonly QUALITY_95: number = 95;
|
||||
private static readonly PERCENT_100: number = 100;
|
||||
|
||||
constructor() {
|
||||
}
|
||||
@ -40,6 +41,15 @@ export class Save {
|
||||
public static async save(item: MediaItem, albumUri: string, optStack: ImageFilterStack, isReplace: Boolean,
|
||||
callback: Function): Promise<void> {
|
||||
Log.info(TAG, `${JSON.stringify(item)} ${isReplace}`);
|
||||
|
||||
// Get quality of image first, making sure it's succeed even in replace mode
|
||||
let finalQuality: number = await this.getImageQuality(item);
|
||||
Log.debug(TAG, "save: final quality = " + finalQuality);
|
||||
let options = {
|
||||
format: 'image/jpeg',
|
||||
quality: finalQuality
|
||||
};
|
||||
|
||||
try {
|
||||
let wrapper = await Loader.loadPixelMapWrapper(item);
|
||||
wrapper = optStack.apply(wrapper);
|
||||
@ -53,16 +63,22 @@ export class Save {
|
||||
return;
|
||||
}
|
||||
|
||||
let options = {
|
||||
format: 'image/jpeg',
|
||||
quality: Save.QUALITY_100
|
||||
};
|
||||
let packer = image.createImagePacker();
|
||||
let buffer = await packer.packing(wrapper.pixelMap, options);
|
||||
Log.info(TAG, 'Format pixelMap data to jpg data end.');
|
||||
|
||||
await fileIO.write(fd, buffer);
|
||||
Log.info(TAG, 'write jpg file end.');
|
||||
try {
|
||||
let imageSourceApi: image.ImageSource = image.createImageSource(fd);
|
||||
await new Promise(async (resolve, reject) => {
|
||||
imageSourceApi.modifyImageProperty('CompressedBitsPerPixel', String(finalQuality / Save.PERCENT_100));
|
||||
resolve(Boolean(true));
|
||||
});
|
||||
} catch (e) {
|
||||
Log.error(TAG, 'sth wrong when set CompressedBitsPerPixel value is ' + (finalQuality / Save.PERCENT_100) +
|
||||
', error is ' + JSON.stringify(e));
|
||||
}
|
||||
let newUri = fileAsset.uri;
|
||||
Log.info(TAG, `New saved file: ${newUri}`);
|
||||
callback && callback(newUri);
|
||||
@ -78,7 +94,6 @@ export class Save {
|
||||
ReportToBigDataUtil.errEventReport(BigDataConstants.PHOTO_EDIT_SAVE_ERROR_ID, msg);
|
||||
callback && callback(undefined);
|
||||
}
|
||||
;
|
||||
}
|
||||
|
||||
private static async createFileAsset(uri: string, albumUri: string, isReplace: Boolean) {
|
||||
@ -111,4 +126,16 @@ export class Save {
|
||||
}
|
||||
return fileAsset;
|
||||
}
|
||||
|
||||
private static async getImageQuality(item: MediaItem): Promise<number> {
|
||||
let quality = undefined;
|
||||
try {
|
||||
quality = await Loader.getCompressedBitsPerPixel(item);
|
||||
} catch (e) {
|
||||
Log.error(TAG, "sth wrong with getCompressedBitsPerPixel" + e);
|
||||
}
|
||||
let finalQuality: number = quality !== undefined ? quality * Save.PERCENT_100 : Save.QUALITY_95
|
||||
Log.debug(TAG, "save: final quality = " + finalQuality);
|
||||
return finalQuality;
|
||||
}
|
||||
}
|
@ -285,6 +285,9 @@ export class PhotoEditCrop extends PhotoEditBase {
|
||||
MathUtils.roundOutRect(crop);
|
||||
let image = this.cropShow.getImageRect();
|
||||
MathUtils.roundOutRect(image);
|
||||
if (this.isFlipHorizontal && this.isFlipVertically && MathUtils.areRectsSame(crop, image)) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
this.isFlipHorizontal !== false ||
|
||||
this.isFlipVertically !== false ||
|
||||
|
Loading…
x
Reference in New Issue
Block a user