• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Media Development
2
3## How do I set a front camera?
4
5Applicable to: OpenHarmony SDK 3.2.3.5, stage model of API version 9
6
71. Set the camera position to **camera.CameraPosition.CAMERA_POSITION_FRONT**.
8
92. Create a **CameraInput** instance based on the camera position and type.
10
11Reference: [Camera Management](../reference/apis/js-apis-camera.md)
12
13Example:
14
15```
16// The rear camera is set by default. You can use **isFrontCamera** to switch to the rear camera.
17let cameraId
18let cameraInput
19for(let cameraIndex = 0; cameraIndex < this.cameraArray.length; cameraIndex++) {
20   let faceType = this.cameraArray[cameraIndex].cameraPosition
21   switch(faceType) {
22  case camera.CameraPosition.CAMERA_POSITION_FRONT: // Front camera
23    if(this.isFrontCamera){
24   cameraId = this.cameraArray[cameraIndex].cameraId
25    }
26	   break
27	 case camera.CameraPosition.CAMERA_POSITION_BACK: // Rear camera
28	   if(!this.isFrontCamera){
29		 cameraId = this.cameraArray[cameraIndex].cameraId
30	   }
31	   break
32	 case camera.CameraPosition.CAMERA_POSITION_UNSPECIFIED:
33	 default:
34	   break
35   }
36}
37cameraInput = await this.cameraManager.createCameraInput(cameraId)
38```
39
40## How do I crop an image?
41
42Applicable to: OpenHarmony SDK 3.2.5.6, stage model of API version 9
43
441. Create an **ImageSource** instance based on the input URI.
45
46   ```
47   let path = this.context.getApplicationContext().fileDirs + "test.jpg";
48   const imageSourceApi = image.createImageSource(path);
49   ```
50
512. Set decoding parameters and decode the image to obtain a **PixelMap** object. Image processing is supported during decoding.
52   - Set **desiredSize** to specify the target size after scaling. If the values are all set to **0**, scaling will not be performed.
53   - Set **desiredRegion** to specify the target rectangular area after cropping. If the values are all set to **0**, cropping will not be performed.
54   - Set **rotateDegrees** to specify the rotation angle. The image will be rotated clockwise at the center.
55
56      ```
57      const decodingOptions = {
58        desiredSize: {
59        height:0,
60        width:0
61        },
62        // Crop a rectangular area.
63        desiredRegion: {
64        size: {
65         height:100,
66         width:100
67        },
68        x:0,
69        y:0
70        },
71        // Rotate the image by 90 degrees.
72        rotate:90
73      }
74      imageSourceApi.createPixelMap(decodingOptions).then(pixelMap => {
75        this.handlePixelMap(pixelMap)
76      })
77      ```
78
793. Process the obtained **PixelMap** object. For example, render and display the image.
80
81## How do I apply for the media read/write permission on a device?
82
83Applicable to: OpenHarmony SDK 3.2.5.5, stage model of API version 9
84
851. Configure the permissions **ohos.permission.READ_MEDIA** and **ohos.permission.WRITE_MEDIA** in the **module.json5** file.
86   Example:
87
88
89   ```
90   {
91     "module" : {
92       "requestPermissions":[
93         {
94           "name" : "ohos.permission.READ_MEDIA",
95           "reason": "$string:reason"
96         },
97         {
98           "name" : "ohos.permission.WRITE_MEDIA",
99           "reason": "$string:reason"
100         }
101       ]
102     }
103   }
104   ```
105
1062. Call **requestPermissionsFromUser** to request the permissions from end users in the form of a dialog box. This operation is required because the grant mode of both permissions is **user_grant**.
107
108   ```
109   let permissions: Array<string> = ['ohos.permission.READ_MEDIA','ohos.permission.WRITE_MEDIA']
110   context.requestPermissionsFromUser(permissions).then((data) => {
111       console.log("Succeed to request permission from user with data: " + JSON.stringify(data))
112   }).catch((error) => {
113       console.log("Failed to request permission from user with error: " + JSON.stringify(error))
114   })
115   ```
116
117## Why can't I play MP4 videos?
118
119Applicable to: OpenHarmony SDK 3.2.7.5, stage model of API version 9
120
121Currently, the system does not support the playback of MP4 videos in H.265 format.
122
123
124## Why can't I play a new video or even encounters a crash after creating more than 10 videos?
125
126Applicable to: OpenHarmony SDK 3.2.7.5, stage model of API version 9
127
128A maximum of 13 media player instances can be created.
129