Resource calling method and device, electronic equipment, storage medium and program product
By parsing resource declaration files and cache resource information during the code compilation period, the problem of poor resource call performance is solved, and efficient resource call and code maintenance improvement is achieved.
Patent Information
- Application Number
- CN202410139229.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-31
- Publication Date
- 2025-08-01
AI Technical Summary
In the prior art, resource calls have problems with poor performance, especially when frequently accessing resource files, and poor code maintenance.
The declaration file of the resource is parsed during the compilation period of the code, and the annotation processor is used to parse the annotation processor to determine the resource information of the resource to be cached, cache the resource information during the runtime, and make resource calls through the mapping of the resource name and resource number identifier.
Reduces the complexity of code writing and maintenance, improves resource call speed, and improves code performance and maintainability.
Smart Images

Figure CN120407093A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technologies, and in particular, to a resource calling method and apparatus, an electronic device, a storage medium, and a program product. Background Art
[0002] During the development of an application program, a large number of resource calls are involved. For example, the resources are layout files, pictures, texts, etc. Usually, during the resource call, it is necessary to obtain the resource number identifier of the resource based on the resource name of the resource, so as to realize the call of the resource based on the resource number identifier. In the related art, there are problems with poor performance in resource calls. Summary of the Invention
[0003] To overcome the problems in the related art, the present disclosure provides a resource calling method and apparatus, an electronic device, a storage medium, and a program product.
[0004] According to a first aspect of an embodiment of the present disclosure, a resource calling method is provided, including:
[0005] During the compilation period of the code, parse the resource declaration file and use an annotation processor to parse the annotation; wherein, the annotation includes indication information for indicating resources to be cached, the declaration file includes resource information of each resource, the resources to be cached are at least part of the resources in the declaration file, and the code is code related to resource calls; during the compilation period of the code, determine the resource information of the resources to be cached from the declaration file based on the parsed indication information;
[0006] During the running period of the code, cache the resource information of the resources to be cached, and call the resources based on the cached resource information.
[0007] In some embodiments, the indication information includes at least part of the identifier of the resource name and / or the resource type; the determining, during the compilation period of the code, the resource information of the resources to be cached from the declaration file based on the parsed indication information includes:
[0008] During the compilation period of the code, use the annotation processor to parse the annotation to obtain at least part of the identifier of the resource name and / or the resource type in the annotation;
[0009] Match the at least part of the identifier and / or the resource type with the resource information of each resource in the declaration file, and determine the resources in the declaration file that match as the resources to be cached.
[0010] In some embodiments, the resource information includes a resource name and a resource number identifier; during the running period of the code, caching the resource information of the resource to be cached and invoking the resource based on the cached resource information includes:
[0011] During the running period of the code, for each resource to be cached, caching the mapping between the resource name and the resource number identifier;
[0012] Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, invoking the resource.
[0013] In some embodiments, during the running period of the code, for each resource to be cached, caching the mapping between the resource name and the resource number identifier includes:
[0014] During the running period of the code, for each resource to be cached, caching the mapping between the resource name and the resource number identifier based on a hash table.
[0015] In some embodiments, the method further includes:
[0016] Obtaining the resource name of the target resource to be invoked;
[0017] The invoking the resource based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching includes:
[0018] Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, determining the resource number identifier corresponding to the resource name of the target resource;
[0019] Implementing the invocation of the target resource based on the resource number identifier of the target resource.
[0020] In some embodiments, the determining the resource number identifier corresponding to the resource name of the target resource based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching includes:
[0021] Instantiating a class method that invokes the mapping of each resource after caching;
[0022] Using the resource name of the target resource as the input parameter of the class method and executing the instantiated class method;
[0023] Based on the execution result of the instantiated class method, determining the resource number identifier of the target resource.
[0024] In some embodiments, the determining the resource number identifier of the target resource based on the execution result of the instantiated class method includes:
[0025] In response to the execution result of the instantiated class method not being a preset value, determine the execution result as the resource number identifier of the target resource;
[0026] In response to the execution result of the instantiated class method being the preset value, look up the resource number identifier of the target resource in the resource declaration file based on the resource name of the target resource.
[0027] In some embodiments, the code includes a watermark resource loading code. During the runtime of the code, cache the resource information of the resource to be cached, and call the resource based on the cached resource information, including:
[0028] In response to the camera application being started and the shooting mode being the watermark mode, run the watermark resource loading code and cache the resource information of all watermark resources;
[0029] Based on the resource information of all cached watermark resources, display a resource list including all watermark resources;
[0030] The method further includes:
[0031] Based on the displayed resource list, determine the target watermark resource;
[0032] In response to detecting a shooting instruction, capture an image;
[0033] Based on the target watermark resource, process the image to obtain a target image with a watermark added.
[0034] According to a second aspect of the embodiments of the present disclosure, there is provided a resource calling device, including:
[0035] A parsing module, configured to parse the resource declaration file during the compilation period of the code and parse the annotation using an annotation processor; wherein, the annotation includes indication information for indicating the resource to be cached, and the declaration file includes the resource information of each resource;
[0036] A first determination module, configured to determine the resource information of the resource to be cached from the declaration file based on the parsed indication information during the compilation period of the code;
[0037] A caching module, configured to cache the resource information of the resource to be cached during the runtime of the code and call the resource based on the cached resource information.
[0038] In some embodiments, the indication information includes at least part of the identifier of the resource name and / or the resource type; the parsing module is further configured to, during the compilation period of the code, parse the annotation by using the annotation processor to obtain at least part of the identifier of the resource name and / or the resource type in the annotation; based on the at least part of the identifier and / or the resource type, match with the resource information of each resource in the declaration file, and determine the resource that matches in the declaration file as the resource to be cached.
[0039] In some embodiments, the resource information includes a resource name and a resource number identifier; the caching module is further configured to, during the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier; based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, call the resource.
[0040] In some embodiments, the caching module is further configured to, during the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier based on a hash table.
[0041] In some embodiments, the apparatus further includes:
[0042] An obtaining module, configured to obtain the resource name of the target resource to be called;
[0043] The caching module is further configured to, based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, determine the resource number identifier corresponding to the resource name of the target resource; and implement the call to the target resource based on the resource number identifier of the target resource.
[0044] In some embodiments, the caching module is further configured to instantiate a class method that calls the mapping of each resource after caching; use the resource name of the target resource as the input parameter of the class method and execute the instantiated class method; and determine the resource number identifier of the target resource based on the execution result of the instantiated class method.
[0045] In some embodiments, the caching module is further configured to, in response to the execution result of the instantiated class method not being a preset value, determine the execution result as the resource number identifier of the target resource; and in response to the execution result of the instantiated class method being the preset value, search for the resource number identifier of the target resource in the declaration file of the resource based on the resource name of the target resource.
[0046] In some embodiments, the code includes watermark resource loading code, and the cache module is further configured to, in response to the startup of the camera application and the shooting mode being the watermark mode, run the watermark resource loading code and cache the resource information of all watermark resources; based on the resource information of all watermark resources after caching, display a resource list including all watermark resources;
[0047] The apparatus further includes:
[0048] A second determination module, configured to determine a target watermark resource based on the displayed resource list;
[0049] An acquisition module, configured to acquire an image in response to detecting a shooting instruction;
[0050] A processing module, configured to process the image based on the target watermark resource to obtain a target image with a watermark added.
[0051] According to a third aspect of the embodiments of the present disclosure, there is provided an electronic device, including:
[0052] A processor;
[0053] A memory for storing computer programs or instructions;
[0054] Wherein, the processor executes the computer program or instructions to implement the steps of the method described in the first aspect above.
[0055] According to a fourth aspect of the embodiments of the present disclosure, there is provided a non-transitory computer-readable storage medium storing computer programs or instructions, and when the computer programs or instructions in the storage medium are executed by a processor, the steps of the method described in the first aspect above are implemented.
[0056] According to a fifth aspect of the embodiments of the present disclosure, there is provided a computer program product including a computer program or instructions, and when the computer program or instructions are executed by a processor, the steps of the method described in the first aspect above are implemented.
[0057] The technical solutions provided by the embodiments of the present disclosure may include the following beneficial effects:
[0058] In the embodiments of the present disclosure, the electronic device determines the resource information of the resources to be cached during the compilation period of the code, and caches the resource information during the running period, so that the electronic device can call the resources based on the cached resource information. On the one hand, determining the resource information of the resources to be cached through annotations during the compilation period can reduce the code writing; and by indicating the resource information through annotations, developers can easily configure the corresponding cached content (changing the indication information in the annotations), so the maintainability of the code is high; on the other hand, when the code runs, the resource information indicated by the annotations will be cached, thus reducing operations such as repeatedly querying the declaration file, improving the performance of the code, and enhancing the calling speed of the resources.
[0059] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] The accompanying drawings herein are incorporated into the specification and constitute a part of the specification, showing embodiments consistent with the present disclosure, and are used together with the specification to explain the principles of the present disclosure.
[0061] Figure 1 is a flowchart of a resource calling method shown according to an exemplary embodiment Figure 1 。
[0062] Figure 2 is a flowchart of a resource calling method shown according to an exemplary embodiment Figure 2 。
[0063] Figure 3 is a block diagram of a resource calling device shown according to an exemplary embodiment.
[0064] Figure 4 is a block diagram of an electronic device shown according to an exemplary embodiment. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0065] Here, the exemplary embodiments will be described in detail, and the examples are shown in the accompanying drawings. When the following description refers to the accompanying drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. On the contrary, they are only examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0066] The resource calling method according to the embodiments of the present disclosure can be applied to an electronic device, which can be various types of terminal devices or servers. Among them, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, a vehicle-mounted intelligent device, etc.; the server can be an independent physical server, or a server cluster or a distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, network services, cloud storage, cloud functions, cloud communications, middleware services, domain name services, security services, content delivery network (CDN) services, and big data and artificial intelligence platforms. The embodiments of the present disclosure do not make any restrictions.
[0067] Figure 1 is a flowchart of a resource calling method shown according to an exemplary embodiment Figure 1 . As Figure 1 shown, the method mainly includes the following steps:
[0068] S11. During the compilation period of the code, parse the resource declaration file, and use an annotation processor to parse the annotations; wherein, the annotations include indication information for indicating resources to be cached, and the declaration file includes resource information of each resource; wherein, the code is code related to resource calling, and the resources to be cached are at least part of the resources in the declaration file;
[0069] S12. During the compilation period of the code, determine the resource information of the resources to be cached from the declaration file based on the parsed indication information;
[0070] S13. During the running period of the code, cache the resource information of the resources to be cached, and call the resources based on the cached resource information.
[0071] In step S11, the electronic device parses the resource declaration file during the compilation period of the code. Here, the code is the code involving resource calls, such as a terminal or server application program that needs to load image resources or file resources, or it can also be a display program for an interface including controls, etc. The resource declaration file is generated during the compilation period of the code. The declaration file includes the resource information of each resource. The resource information of each resource includes at least the resource name and the resource number identifier (Identity Document, ID). Both the resource name and the resource number identifier are used to uniquely identify the resource. Among them, the resource name is created by the developer in the program and is the variable name of the resource. For example, the resource name of a resource in a layout file is "my_button"; while the resource number identifier is generated during the compilation period of the code. The resource number identifier is an integer value and is used to uniquely identify the resource in the code. For example, the resource number identifier corresponding to "my_button" is the hexadecimal data "0x7f080668". The code can further execute the code corresponding to the resource based on the resource number identifier during the running period. For example, perform settings of control attributes, etc.
[0072] In the embodiments of the present disclosure, the declaration file may further include the resource type, the data type corresponding to the resource number identifier, etc. Taking the resource declaration file as the R class file in the Android system as an example, the resource declaration file can be R.txt. The electronic device can generate R.txt during the compilation period and then parse the resource information corresponding to each resource therein.
[0073] Exemplarily, "int drawable ic_wp_amber_fort 0x7f080698" is the resource information of a resource in the declaration file. Among them, "int" indicates that the data type of the resource number identifier is an integer type, "drawable" indicates that the resource is a picture type of resource; "ic_wp_amber_fort" represents the resource name; "0x7f080698" represents the resource number identifier.
[0074] In the embodiments of the present disclosure, the electronic device also uses an annotation processor to parse annotations. Among them, the annotation processor can be a Java-based (Annotation Processing Tool, APT) annotation processor, which is used to scan and process annotations during compilation; the annotation can be, for example, an annotation based on @ResourceNameCache, which can be used to cache the resource name. By marking a method or a field, it indicates that the value of this method or field should be cached so that it can be quickly obtained when used later, instead of recalculating or obtaining it every time.
[0075] Exemplarily, the annotation is: @ResourceNameCache(className = "AiWatermarkResources", type = "drawable", regex = "ic_w[pr]_.*"); where, className is used to identify the class name of the cached resource; type is used to indicate the type of the resource to be cached, for example, "drawable" is an image or graphic resource; regex is the indication information of the resource name, such as "ic_w[pr]_.*" indicating the resources whose names start with ic_wp_ and ic_wr_. In the embodiments of the present disclosure, the electronic device can parse the above annotation using an annotation processor to obtain the information of each of the above fields, that is, the indication information of the resource to be cached, and the resource to be cached is at least part of the resources in the declaration file.
[0076] In step S12, the electronic device determines the resource information of the resource to be cached from the declaration file based on the parsed indication information during the compilation period of the code. In some embodiments, the electronic device can match the indication information with the resource information in the declaration file to determine the resource information of the resource to be cached; in other embodiments, the electronic device can determine the resource information mapped by the indication information based on a preset mapping table.
[0077] In step S13, the electronic device caches the resource information of the resource to be cached during the running period of the code. For example, the cache can exist in the memory of the electronic device. When the resource information is requested, the electronic device first checks whether the required data already exists in the cache. If it exists (this situation is called "cache hit"), the electronic device can directly read the data from the cache. In the embodiments of the present disclosure, the electronic device calls the resource based on the cached resource information, for example, determines the resource number identifier corresponding to the resource name from the cached resource information based on the resource name, so as to execute relevant code based on the resource number identifier, such as image loading or control property setting, etc.
[0078] Generally, where it is necessary to obtain the resource number identifier through the resource name, for example, in the camera intelligent watermark adding mode, there are a large number of image resources used. These resources use the watermark name in the watermark configuration field of the xml configuration file, plus a fixed prefix as the resource name of the image. When loading the image, it is then necessary to obtain the corresponding resource number identifier through these resource names.
[0079] In the related art, during the runtime of the Android system, based on android.content.res.Resources#getIdentifier, the resource ID corresponding to a given resource name is found from the application's resource file (i.e., the resource declaration file). However, due to frequent access to the resource file, the use of a relatively large number of resources will seriously affect performance. In addition, there is also a solution to write code to store the one-to-one mapping of the resource name and the resource number identifier into a Map. This mapping will generate a large amount of code and is not conducive to maintenance. Any addition or deletion requires operation by code maintenance personnel.
[0080] In contrast, in the embodiments of the present disclosure, the electronic device determines the resource information of the resources to be cached during the compilation period of the code and caches the resource information during the runtime, so that the electronic device can call the resources based on the cached resource information. On the one hand, determining the resource information of the resources to be cached through annotations during the compilation period can reduce code writing; and by indicating the resource information through annotations, developers can easily configure the corresponding cached content (changing the indication information in the annotations), so the maintainability of the code is high; on the other hand, when the code runs, the resource information indicated by the annotations will be cached, thereby reducing operations such as repeatedly querying the declaration file, improving the performance of the code, and enhancing the calling speed of the resources.
[0081] In some embodiments, the indication information includes at least part of the identifier of the resource name and / or the resource type; the determining, during the compilation period of the code, the resource information of the resources to be cached from the declaration file based on the parsed indication information includes:
[0082] During the compilation period of the code, use the annotation processor to parse the annotation to obtain at least part of the identifier of the resource name and / or the resource type in the annotation;
[0083] Based on the at least part of the identifier and / or the resource type, match with the resource information of each resource in the declaration file, and determine the resource that matches in the declaration file as the resource to be cached.
[0084] In an embodiment of the present disclosure, the indication information in the annotation includes at least part of the identifier of the resource name and / or the resource type. For example, the aforementioned "ic_wp_" and "ic_wr" are the prefixes of the resource name, and "drawable" indicates the resource type. The electronic device parses the annotation through an annotation processor to obtain at least part of the identifier and / or the resource type, and thus can match the resource information of each resource in the declaration file based on the indication information. For example, after matching based on "ic_wp_" and "ic_wr" with the resource information of each resource in the declaration file, and / or after matching based on "drawable", the above "int drawable ic_wp_amber_fort 0x7f080698" is the resource information of the resource to be cached.
[0085] It should be noted that in an embodiment of the present disclosure, the electronic device can use a regular matching method to determine the resource information of the resource to be cached from the declaration file based on the indication information. In a regular expression, various special characters and grammars can be used to define the way of matching the search pattern. For example, a complete match of characters can be performed based on "drawable", or a partial match of characters can be performed based on "ic_w[pr]_.*".
[0086] It can be understood that in an embodiment of the present disclosure, the electronic device determines the resource to be cached by matching the indication information with the resource information of each resource in the declaration file. The solution is simple and can improve the running performance of the code.
[0087] Figure 2 This is a flow of a resource calling method in an embodiment of the present disclosure Figure 2 The resource information includes a resource name and a resource number identifier. The above Figure 1 The step S13 in may include:
[0088] S13a. During the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier;
[0089] S13b. Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, call the resource.
[0090] In an embodiment of the present disclosure, when the electronic device caches the resource information of the resource to be cached, what is cached is the resource name and the resource number identifier used to identify the resource, rather than the entire resource information in the declaration file. In addition, what the electronic device caches is the mapping between the resource name and the resource number identifier. For example, the mapping between the aforementioned "ic_wp_amber_fort" and "0x7f080698" is established.
[0091] It should be noted that in the implementation of the present disclosure, the electronic device can create a method class to generate a mapping between the resource name and the resource number identifier of the resource to be cached, and the method of establishing the mapping can adopt the class method supported by the operating system in the electronic device or the third-party library function, and the embodiments of the present disclosure do not limit this. Exemplarily, the electronic device can cache the mapping between the resource name and the resource number identifier based on the TreeMap method in the Android operating system.
[0092] It can be understood that in the embodiments of the present disclosure, the electronic device caches the mapping between the resource name and the resource number identifier corresponding to each resource for resource invocation. On the one hand, only caching the necessary information can reduce the caching time and the memory occupation of the cache, thereby improving the performance of resource invocation; on the other hand, when caching, caching the mapping relationship also helps to quickly find the resource number identifier based on the resource name when invoking the resource, so it can also improve the speed of resource invocation.
[0093] In some embodiments, during the running period of the code, for each resource to be cached, caching the mapping between the resource name and the resource number identifier includes:
[0094] During the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier based on a hash table.
[0095] In the embodiments of the present disclosure, the electronic device caches the mapping in the form of a hash table. As mentioned above, the electronic device can create a method class to generate a mapping between the resource name and the resource number identifier of the resource to be cached. In the embodiments of the present disclosure, taking the Android system as an example, the electronic device can create a cache implementation class through javapoet, such as the AiWatermarkResources class. This class includes an init method, which is used to generate a mapping between the resource name and the resource number identifier based on HashMap.
[0096] For example, the electronic device creates a HashMap container in the init method: mResources = new HashMap<>(16), and saves the resource information of the resource to be cached into this container through the put method. For example, the resource information of a certain resource is cached as follows: mResources.put("ic_wp_amber_fort", R.drawable.ic_wp_amber_fort). Among them, "ic_wp_amber_fort" is the resource name, and R.drawable.ic_wp_amber_fort is the resource number identifier returned based on the R class method, corresponding to the aforementioned "0x7f080698".
[0097] It should be noted that in the embodiments of the present disclosure, since the electronic device has obtained the resource information of the resources to be cached during the compilation period of the code, and the electronic device knows the number of resources to be cached, when the electronic device creates a HashMap container, it can create the size of the container according to the known number of resources to be cached. For example, in the above "new HashMap<>(16)", 16 is a value slightly larger than the number of resources to be cached.
[0098] It can be understood that in the embodiments of the present disclosure, based on the mapping between the resource name and the resource number identifier cached for each resource, on the one hand, since the hash table has a faster query speed compared to other mapping methods, it can improve the speed of subsequent resource calls; on the other hand, since the hash table can customize the container size, the electronic device can create the container size according to the known number of resources to be cached, so it can reduce the waste of memory, which is beneficial to the performance of code operation.
[0099] In some embodiments, the method further includes:
[0100] Obtain the resource name of the target resource to be called;
[0101] The calling of the resources based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching includes:
[0102] Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, determine the resource number identifier corresponding to the resource name of the target resource;
[0103] Implement the calling of the target resource based on the resource number identifier of the target resource.
[0104] In the embodiments of the present disclosure, when the electronic device calls resources based on the resource information of the resources to be cached, as described above, it is necessary to obtain the resource number identifier to call the resources. For this, in the embodiments of the present disclosure, the electronic device obtains the resource name of the target resource to be called, and based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, determines the resource number identifier corresponding to the resource name of the target resource.
[0105] It can be understood that since the method of determining the resource number identifier corresponding to the resource name of the target resource based on the mapping is a way of looking up a table, the calling of the resources can be quickly realized through this method.
[0106] In some embodiments, the determining the resource number identifier corresponding to the resource name of the target resource based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching includes:
[0107] Instantiate the class method that maps each resource after calling the cache;
[0108] Use the resource name of the target resource as the input parameter of the class method and execute the instantiated class method;
[0109] Based on the execution result of the instantiated class method, determine the resource number identifier of the target resource.
[0110] In the embodiments of the present disclosure, the electronic device may also create a tool class that uses the cache, such as the ResourcesCacheUtils class. Through various methods included in this class, the cached resources can be obtained through reflection. Among them, this class includes a class method that maps each resource after calling the cache. The electronic device can implement the call of the resource based on this class method. Exemplarily, this class method may be:
[0111] public Integer getResourcesId(String resourceName){
[0112] return mResources.get(resourceName);
[0113] }
[0114] In the above class method, the input parameter is the resource name "resourceName", and the return value is "mResources.get(resourceName)", which represents the resource number identifier; among them, mResources refers to the aforementioned cached mapping.
[0115] Based on the above class method, it can be seen that the resource name of the target resource to be called needs to be passed in, and the corresponding resource number identifier can be returned based on the cached mapping. When the above class method is called, instantiation needs to be performed first. Exemplarily, instantiating this class method with the cache variable can be cache.getResourcesId(name). Based on the instantiated class method, the electronic device can obtain the execution result of the instantiated class method by passing in the resource name of the target resource.
[0116] It should be noted that in the embodiments of the present disclosure, if the resource information of the target resource exists in the cached mapping, the electronic device may determine the return value after executing the instantiated class method as the resource number identifier of the target resource.
[0117] It can be understood that in the embodiments of the present disclosure, the electronic device determines the resource number identifier corresponding to the resource name of the target resource by calling the class method that maps each resource after calling the cache, which is beneficial to the maintenance during code development, saves memory space, and improves the execution efficiency of the program.
[0118] It should be noted that, in the embodiment of the present disclosure, the tool class using the cache may also include a method class for obtaining a resource number identifier based on the resource type. Through this method class, after the resource type is passed in, the electronic device may return the resource number identifier belonging to that type of resource in the cache.
[0119] In some embodiments, determining the resource number identifier of the target resource based on the execution result of the instantiated class method includes:
[0120] In response to an execution result of the instantiated class method not being a preset value, determining the execution result as a resource number identifier of the target resource;
[0121] In response to the execution result of the instantiated class method being the preset value, searching for the resource number identifier of the target resource in the declaration file of the resource based on the resource name of the target resource.
[0122] In an embodiment of the present disclosure, the preset value may be "null". When the execution result of the instantiated class method is not empty, the electronic device directly determines the execution result as the resource number identifier of the target resource, which indicates that the resource exists in the cached mapping.
[0123] In the disclosed embodiment, if the execution result of the instantiated class method is null, it indicates that the resource does not exist in the cached mapping. In this case, the electronic device uses the original method to search the resource number identifier of the target resource in the resource declaration file based on the resource name of the target resource, for example, using the aforementioned getIdentifier method to determine the resource number identifier of the target resource from the declaration file.
[0124] It can be understood that in the embodiment of the present disclosure, the electronic device specifically determines the resource number identifier of the target resource based on whether the execution result of the instantiated class method is a preset value, so that the electronic device can increase the probability of successfully calling the target resource.
[0125] In some embodiments, the code includes a watermark resource loading code, and during the execution of the code, the resource information of the resource to be cached is cached, and the resource is called based on the cached resource information, including:
[0126] In response to the camera application being started and the camera mode being the watermark mode, running the watermark resource loading code and caching resource information of all watermark resources;
[0127] Based on the resource information of all watermark resources after caching, a resource list including all watermark resources is displayed;
[0128] The method further includes:
[0129] Determine a target watermark resource based on the displayed resource list;
[0130] In response to detecting a photographing instruction, capture an image;
[0131] Process the image based on the target watermark resource to obtain a target image with the watermark added.
[0132] In the embodiments of the present disclosure, the electronic device includes a camera application and supports the watermark mode. When taking a picture in the watermark mode, a watermark resource will be automatically added to the captured picture. For example, the watermark resource is a picture or text resource, and the above watermark resource is stored in the electronic device. In the embodiments of the present disclosure, after the electronic device starts the camera application, it can take pictures in the watermark mode based on the user's settings.
[0133] In the embodiments of the present disclosure, when the electronic device takes a picture in the watermark mode, it will start the watermark resource loading code. This code determines the resources to be cached during compilation. In this embodiment, the resources to be cached are all watermark resources, such as all the watermark resources indicated by the aforementioned "drawable" and "ic_w[pr]_.*". After compilation, during the running period of the watermark resource loading code, the electronic device can, for example, cache all watermark resources based on the aforementioned hash table mapping method for subsequent invocation of the watermark resources. In the embodiments of the present disclosure, when invoking all the cached watermark resources, for example, the electronic device can display the watermark resources based on the resource number identifiers of each watermark resource and display the watermark resources in a list form. For example, for picture resources, what is displayed in the list can be the picture content of the watermark resource or the name of the picture (such as cloud.jpg), etc.
[0134] In the embodiments of the present disclosure, based on the displayed resource list, the user of the electronic device can select the resources in the list. For example, the electronic device receives a voice instruction for selecting a target watermark resource or a touch selection instruction for the target watermark resource to determine the target watermark resource. Subsequently, after the electronic device detects a photographing instruction and captures an image, it can process the image based on the target watermark resource. For example, add the target watermark resource to the image to obtain a target image with the watermark resource after taking the picture.
[0135] It can be understood that in the embodiments of the present disclosure, when the photographing mode of the camera application of the electronic device is the watermark mode, the aforementioned resource invocation method is used to load the watermark resources, so that the electronic device can quickly add a watermark to the captured image to generate a target image, which can improve the photographing performance in the watermark mode and enhance the user experience.
[0136] Figure 3It is a block diagram of a resource calling device shown according to an exemplary embodiment. As Figure 3 shown, the device mainly includes:
[0137] A parsing module 301, configured to parse a resource declaration file during the compilation period of the code and parse annotations using an annotation processor; wherein, the annotations include indication information for indicating resources to be cached, the declaration file includes resource information of each resource, the resources to be cached are at least part of the resources in the declaration file, and the code is code related to resource calling;
[0138] A first determination module 302, configured to determine, during the compilation period of the code, resource information of resources to be cached from the declaration file based on the parsed indication information;
[0139] A caching module 303, configured to cache resource information of the resources to be cached during the running period of the code and call resources based on the cached resource information.
[0140] In some embodiments, the indication information includes at least part of an identifier of a resource name and / or a resource type; the parsing module 301 is further configured to, during the compilation period of the code, parse the annotations using the annotation processor to obtain at least part of an identifier of a resource name and / or a resource type in the annotations; based on the at least part of the identifier and / or the resource type, match with the resource information of each resource in the declaration file, and determine the resources that match in the declaration file as the resources to be cached.
[0141] In some embodiments, the resource information includes a resource name and a resource number identifier; the caching module 302 is further configured to, during the running period of the code, cache a mapping between a resource name and a resource number identifier for each resource to be cached; based on the mapping between the resource name and the resource number identifier corresponding to each cached resource, call resources.
[0142] In some embodiments, the caching module 303 is further configured to, during the running period of the code, cache a mapping between a resource name and a resource number identifier for each resource to be cached based on a hash table.
[0143] In some embodiments, the device further includes:
[0144] An acquisition module, configured to acquire a resource name of a target resource to be called;
[0145] The cache module 303 is further configured to determine the resource number identifier corresponding to the resource name of the target resource based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching; and implement the call of the target resource based on the resource number identifier of the target resource.
[0146] In some embodiments, the cache module 303 is further configured to instantiate a class method that calls the mapping of each resource after caching; use the resource name of the target resource as the input parameter of the class method and execute the instantiated class method; and determine the resource number identifier of the target resource based on the execution result of the instantiated class method.
[0147] In some embodiments, the cache module 303 is further configured to, in response to the execution result of the instantiated class method not being a preset value, determine the execution result as the resource number identifier of the target resource; and in response to the execution result of the instantiated class method being the preset value, search for the resource number identifier of the target resource in the declaration file of the resource based on the resource name of the target resource.
[0148] In some embodiments, the code includes watermark resource loading code. The cache module 303 is further configured to, in response to the startup of the camera application and the photographing mode being the watermark mode, run the watermark resource loading code and cache the resource information of all watermark resources; and display a resource list including all watermark resources based on the resource information of all watermark resources after caching.
[0149] The device further includes:
[0150] A second determination module, configured to determine a target watermark resource based on the displayed resource list;
[0151] An acquisition module, configured to acquire an image in response to detecting a photographing instruction;
[0152] A processing module, configured to process the image based on the target watermark resource to obtain a target image with a watermark added.
[0153] Regarding the device in the above embodiments, the specific manners in which each module performs operations have been described in detail in the embodiments related to the method, and will not be elaborated here in detail.
[0154] Figure 4 It is a structural block diagram of an electronic device 400 shown according to an exemplary embodiment. For example, the device 400 may be a mobile phone, a computer, a digital broadcast terminal, a messaging device, a game console, a tablet device, a medical device, a fitness device, a personal digital assistant, etc.
[0155] Refer to Figure 4, the apparatus 400 may include one or more of the following components: a processing component 402, a memory 404, a power component 406, a multimedia component 408, an audio component 410, an input / output (I / O) interface 412, a sensor component 414, and a communication component 416.
[0156] The processing component 402 generally controls the overall operation of the apparatus 400, such as operations associated with at least one of display, telephone call, data communication, camera operation, and recording operation. The processing component 402 may include one or more processors 420 to execute instructions to complete all or part of the steps of the above methods. In addition, the processing component 402 may include one or more modules to facilitate the interaction between the processing component 402 and other components. For example, the processing component 402 may include a multimedia module to facilitate the interaction between the multimedia component 408 and the processing component 402.
[0157] The memory 404 is configured to store various types of data to support the operations on the apparatus 400. Examples of such data include at least one of the following: instructions for any application or method operating on the apparatus 400, contact data, phone book data, messages, pictures, and videos. The memory 404 may be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, a magnetic disk, or an optical disk.
[0158] The power component 406 provides power to the various components of the apparatus 400. The power component 406 may include at least one of the following: a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power for the apparatus 400.
[0159] The multimedia component 408 includes a screen that provides an output interface between the device 400 and the user. In some embodiments, the screen may include a Liquid Crystal Display (LCD) and a Touch Panel (TP). If the screen includes a touch panel, the screen can be implemented as a touch screen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors can sense not only the boundaries of the touch or swipe actions but also detect the duration and pressure associated with the touch or swipe operations. In some embodiments, the multimedia component 408 includes a front camera and / or a rear camera. When the device 400 is in an operating mode, such as a shooting mode or a video mode, the front camera and / or the rear camera can receive external multimedia data. Each of the front camera and the rear camera can be a fixed optical lens system or have a focal length and optical zoom capabilities.
[0160] The audio component 410 is configured to output and / or input audio signals. For example, the audio component 410 includes a Microphone (MIC), which is configured to receive external audio signals when the device 400 is in an operating mode, such as a call mode, a recording mode, and a voice recognition mode. The received audio signals can be further stored in the memory 404 or transmitted via the communication component 416. In some embodiments, the audio component 410 further includes a speaker for outputting audio signals.
[0161] The I / O interface 412 provides an interface between the processing component 402 and the peripheral interface module, which can be a keyboard, a click wheel, buttons, etc. These buttons can include but are not limited to: a home button, a volume button, a power button, and a lock button.
[0162] The sensor assembly 414 includes one or more sensors for providing an assessment of the status of the device 400 in various aspects. For example, the sensor assembly 414 can detect the on / off state of the device 400, the relative positioning of components, such as the display and keypad of the device 400. The sensor assembly 414 can also detect a change in the position of the device 400 or a component in the device 400, the presence or absence of user contact with the device 400, the orientation or acceleration / deceleration of the device 400, and the temperature change of the device 400. The sensor assembly 414 can include a proximity sensor configured to detect the presence of nearby objects without any physical contact. The sensor assembly 414 can also include a light sensor, such as a Complementary Metal Oxide Semiconductor (CMOS) or Charge Coupled Device (CCD) image sensor, for use in imaging applications. In some embodiments, the sensor assembly 414 can also include, but is not limited to, at least one of the following: an acceleration sensor, a gyroscope sensor, a magnetic sensor, a pressure sensor, and a temperature sensor.
[0163] The communication component 416 is configured to facilitate communication between the device 400 and other devices in a wired or wireless manner. The device 400 can access a wireless network based on communication standards, such as Wi-Fi, 4G, 5G, or a combination thereof. In an exemplary embodiment, the communication component 416 receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component 416 further includes a Near Field Communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on Radio Frequency Identification (RFID) technology, Infrared Data Association (IrDA) technology, Ultra Wide Band (UWB) technology, Bluetooth (BT) technology, and other technologies.
[0164] In an exemplary embodiment, the apparatus 400 may be implemented by one or more application specific integrated circuits (ASICs), digital signal processors (DSPs), digital signal processing devices (DSPDs), programmable logic devices (PLDs), field programmable gate arrays (FPGAs), controllers, microcontrollers, microprocessors, or other electronic components.
[0165] In an exemplary embodiment, a non-transitory computer-readable storage medium including instructions is also provided, such as a memory 404 including executable instructions or a computer program. The above instructions or computer program can be executed by a processor 420 of the apparatus 400 to implement the above method. For example, the non-transitory computer-readable storage medium may be a ROM, a random access memory (RAM), a compact disc read-only memory (CD-ROM), magnetic tape, floppy disk, and optical data storage device, etc.
[0166] A non-transitory computer-readable storage medium, when the instructions in the storage medium are executed by a processor of an electronic device, enables the electronic device to execute any one of the resource invocation methods in the above embodiments of the present disclosure.
[0167] The embodiments of the present disclosure provide a computer program product, which includes: a computer program or executable instructions, and the computer program or executable instructions are stored in a computer-readable storage medium. A processor of a computer device reads the computer program or executable instructions from the computer-readable storage medium, and the processor executes the computer program or executable instructions, so that the computer device executes any one of the resource invocation methods in the above embodiments of the present disclosure.
[0168] Those skilled in the art will readily conceive of other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses, or adaptations of the present disclosure, which follow the general principles of the present disclosure and include common general knowledge or conventional technical means in the technical field not disclosed in the present disclosure. The specification and embodiments are only to be considered as exemplary, and the true scope and spirit of the present disclosure are pointed out by the following claims.
[0169] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the present disclosure is only limited by the appended claims.
Claims
1. A resource calling method, characterized in that, Including: During the compilation period of the code, parse the resource declaration file and use an annotation processor to parse the annotations; wherein, the annotations include indication information for indicating resources to be cached, the declaration file includes resource information of each resource, the resources to be cached are at least part of the resources in the declaration file, and the code is code related to resource invocation; During the compilation period of the code, determine the resource information of the resources to be cached from the declaration file based on the parsed indication information; During the running period of the code, cache the resource information of the resources to be cached and invoke the resources based on the cached resource information.
2. The method according to claim 1, wherein The indication information includes at least part of the identifier of the resource name and / or the resource type; the determining, during the compilation period of the code, the resource information of the resources to be cached from the declaration file based on the parsed indication information includes: During the compilation period of the code, use the annotation processor to parse the annotations to obtain at least part of the identifier of the resource name and / or the resource type in the annotations; Based on the at least part of the identifier and / or the resource type, match with the resource information of each resource in the declaration file, and determine the resources that match in the declaration file as the resources to be cached.
3. The method according to claim 1 or 2, characterized in that, The resource information includes the resource name and the resource number identifier; the caching, during the running period of the code, the resource information of the resources to be cached and invoking the resources based on the cached resource information includes: During the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier; Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, invoke the resources.
4. The method according to claim 3, wherein The caching, during the running period of the code, for each resource to be cached, the mapping between the resource name and the resource number identifier includes: During the running period of the code, for each resource to be cached, cache the mapping between the resource name and the resource number identifier based on a hash table.
5. The method according to claim 3, wherein The method further includes: Obtain the resource name of the target resource to be invoked; The invoking the resources based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching includes: Based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, determine the resource number identifier corresponding to the resource name of the target resource; Based on the resource number identifier of the target resource, implement the invocation of the target resource.
6. The method according to claim 5, characterized in that, The determining, based on the mapping between the resource name and the resource number identifier corresponding to each resource after caching, the resource number identifier corresponding to the resource name of the target resource includes: Instantiate and call the class method that caches the mapping of each resource; Use the resource name of the target resource as the input parameter of the class method and execute the instantiated class method; Based on the execution result of the instantiated class method, determine the resource number identifier of the target resource.
7. The method according to claim 6, wherein The determining, based on the execution result of the instantiated class method, the resource number identifier of the target resource includes: In response to the execution result of the instantiated class method not being a preset value, determine the execution result as the resource number identifier of the target resource; In response to the execution result of the instantiated class method being the preset value, look up the resource number identifier of the target resource in the resource declaration file based on the resource name of the target resource.
8. The method according to claim 1, wherein The code includes watermark resource loading code. During the runtime of the code, caching the resource information of the resource to be cached, and invoking the resource based on the cached resource information includes: In response to the camera application starting and the shooting mode being the watermark mode, run the watermark resource loading code and cache the resource information of all watermark resources; Based on the cached resource information of all watermark resources, display a resource list including all watermark resources; The method further includes: Based on the displayed resource list, determine the target watermark resource; In response to detecting a shooting instruction, capture an image; Process the image based on the target watermark resource to obtain a target image with the watermark added.
9. A resource calling device, characterized in that, Includes: A parsing module, configured to parse the resource declaration file during the compilation period of the code and parse the annotations using an annotation processor; wherein, the annotations include indication information for indicating the resource to be cached, the declaration file includes the resource information of each resource, the resource to be cached is at least part of the resources in the declaration file, and the code is code related to resource invocation; A first determination module, configured to determine the resource information of the resource to be cached from the declaration file based on the parsed indication information during the compilation period of the code; A caching module, configured to cache the resource information of the resource to be cached during the runtime of the code and invoke the resource based on the cached resource information.
10. An electronic device, characterized in that, Includes: A processor; A memory for storing computer programs or instructions; Wherein, the processor executes the computer program or instructions to implement the steps of the method according to any one of claims 1 to 8.
11. A non - transitory computer - readable storage medium storing a computer program or instructions, characterized in that, When the computer program or instructions in the storage medium are executed by the processor, the steps of the method according to any one of claims 1 to 8 are implemented.
12. A computer program product, comprising a computer program or instructions, characterized in that, When the computer program or instructions are executed by the processor, the steps of the method according to any one of claims 1 to 8 are implemented.