1/* 2 * Copyright (c) 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 */ 15import image from '@ohos.multimedia.image'; 16import MediaLib from '@ohos.multimedia.mediaLibrary'; 17import mediaModel from '../model/MediaModel'; 18import { Log } from '../utils/Log'; 19import { MediaConstants } from '../constants/MediaConstants'; 20import { getResourceString } from '../utils/ResourceUtils'; 21import { SimpleAlbumDataItem } from '../data/SimpleAlbumDataItem'; 22 23const TAG = "MediaDataHelper" 24 25export class Rotatable { 26 rotatable: boolean 27 orientation: number 28} 29 30export async function setOrientation(fileAsset: MediaLib.FileAsset, orientation: number): Promise<void> { 31 Log.info(TAG, `setOrientation`) 32 try { 33 let fd: number = await mediaModel.openAsset('RW', fileAsset); 34 let imageSourceApi: image.ImageSource = image.createImageSource(fd); 35 await imageSourceApi.modifyImageProperty("Orientation", getPropertyValidOrientation(orientation)); 36 imageSourceApi.release(); 37 mediaModel.closeAsset(fd, fileAsset); 38 } catch (err) { 39 Log.error(TAG, `setOrientation err ${JSON.stringify(err)}`); 40 fileAsset.orientation = orientation; 41 await fileAsset.commitModify(); 42 } 43} 44 45function getPropertyValidOrientation(orientation: number): string { 46 Log.info(TAG, `getPropertyValidOrientation ${orientation}`); 47 switch (orientation) { 48 case MediaConstants.ROTATE_NONE: 49 return "1"; 50 case MediaConstants.ROTATE_THIRD: 51 return "8"; 52 case MediaConstants.ROTATE_TWICE: 53 return "3"; 54 case MediaConstants.ROTATE_ONCE: 55 return "6"; 56 default: 57 return ""; 58 } 59} 60 61export async function getAlbumDisplayName(name: string): Promise<string> { 62 switch (name) { 63 case MediaConstants.ALBUM_ID_ALL: 64 return await getResourceString($r('app.string.album_all')); 65 case MediaConstants.ALBUM_ID_VIDEO: 66 return await getResourceString($r('app.string.album_video')); 67 case MediaConstants.ALBUM_ID_RECYCLE: 68 return await getResourceString($r('app.string.album_recycle')); 69 case MediaConstants.ALBUM_ID_CAMERA: 70 return await getResourceString($r('app.string.album_camera')); 71 case MediaConstants.ALBUM_ID_FAVOR: 72 return await getResourceString($r('app.string.album_favor')); 73 case MediaConstants.ALBUM_ID_REMOTE: 74 return await getResourceString($r('app.string.album_remote_device')); 75 case MediaConstants.ALBUM_ID_SNAPSHOT: 76 return await getResourceString($r('app.string.album_screen_shot')); 77 default: 78 break; 79 } 80 return null; 81} 82 83export async function getFetchOptions(selectType: number, albumId: string, deviceId: string): Promise<MediaLib.MediaFetchOptions> { 84 let selections: string = ""; 85 let selectionArgs: Array<string> = []; 86 let order: string = `date_added DESC`; 87 if (selectType == MediaConstants.SELECT_TYPE_VIDEO || albumId == MediaConstants.ALBUM_ID_VIDEO) { 88 selections = MediaLib.FileKey.MEDIA_TYPE + ' = ?'; 89 selectionArgs = [MediaLib.MediaType.VIDEO.toString()]; 90 } else if (selectType == MediaConstants.SELECT_TYPE_IMAGE && albumId != MediaConstants.ALBUM_ID_VIDEO) { 91 selections = MediaLib.FileKey.MEDIA_TYPE + ' = ?'; 92 selectionArgs = [MediaLib.MediaType.IMAGE.toString()]; 93 } else if (selectType == MediaConstants.SELECT_TYPE_IMAGE && albumId == MediaConstants.ALBUM_ID_VIDEO) { 94 return undefined; 95 } else { 96 selections = MediaLib.FileKey.MEDIA_TYPE + ' = ? or ' + MediaLib.FileKey.MEDIA_TYPE + ' = ?'; 97 selectionArgs = [MediaLib.MediaType.IMAGE.toString(), MediaLib.MediaType.VIDEO.toString()]; 98 } 99 if (albumId == MediaConstants.ALBUM_ID_CAMERA) { 100 let path = await mediaModel.getPublicDirectory(MediaLib.DirectoryType.DIR_CAMERA); 101 selections = `(${selections}) and ${MediaLib.FileKey.ALBUM_NAME} = ?`; 102 selectionArgs.push(path.substr(0, path.length - 1)); 103 } else if (albumId == MediaConstants.ALBUM_ID_SNAPSHOT) { 104 let path = await mediaModel.getPublicDirectory(MediaLib.DirectoryType.DIR_IMAGE) + "Screenshots/"; 105 selections = `(${selections}) and ${MediaLib.FileKey.RELATIVE_PATH} = ?`; 106 selectionArgs.push(path); 107 } else if ((new Number(albumId).valueOf() || 0) > 0) { 108 selections = `(${selections}) and ${MediaLib.FileKey.ALBUM_ID} = ?`; 109 selectionArgs.push(albumId); 110 } 111 112 let fetchOption: MediaLib.MediaFetchOptions = { 113 selections: selections, 114 selectionArgs: selectionArgs, 115 order: order 116 }; 117 118 if (deviceId.length > 0) { 119 fetchOption['networkId'] = deviceId; 120 } 121 return fetchOption; 122} 123 124export async function getFetchOptionsByItem(item: SimpleAlbumDataItem): Promise<MediaLib.MediaFetchOptions> { 125 let selections: string = `${MediaLib.FileKey.MEDIA_TYPE} = ? or ${MediaLib.FileKey.MEDIA_TYPE} = ?`; 126 let selectionArgs: string[] = [MediaLib.MediaType.IMAGE.toString(), MediaLib.MediaType.VIDEO.toString()]; 127 128 if (item.displayName.length > 0) { 129 selections = `(${selections}) and ${MediaLib.FileKey.DISPLAY_NAME} = ?`; 130 selectionArgs.push(item.displayName); 131 } 132 133 if (item.relativePath.length > 0) { 134 selections = `(${selections}) and ${MediaLib.FileKey.RELATIVE_PATH} = ?`; 135 selectionArgs.push(item.relativePath); 136 } 137 138 let fetchOption: MediaLib.MediaFetchOptions = { 139 selections: selections, 140 selectionArgs: selectionArgs, 141 order: `date_added DESC` 142 }; 143 return fetchOption; 144} 145