Disclosure of Invention
Therefore, it is necessary to provide a method, an apparatus, a computer device and a storage medium for cropping an initial image based on a preset reference size, so as to automatically crop an image meeting a preset size requirement, and make image cropping more intelligent.
A method of image generation, the method comprising:
the method comprises the steps of obtaining the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size;
determining the size of a first reference frame according to the first size and a preset reference size;
determining a second size according to the size of the first reference frame, the first size and the screen size;
and acquiring an initial image, and cutting the initial image according to the second size to obtain a target image file.
In one embodiment, the selecting a first size matching the screen size from a preset image size list according to the screen size includes:
acquiring each photo size in the image size list;
selecting a plurality of candidate photo sizes corresponding to the height of the photo size smaller than the height of the screen size and the width of the photo size smaller than the width of the screen size;
calculating a first ratio of width to height in each candidate photo size;
and comparing each first ratio with a second ratio of the width to the height in the screen size to obtain a first ratio with the minimum difference value with the second ratio, and taking the candidate photo size corresponding to the first ratio as the first size.
In one embodiment, the determining the size of the first reference frame according to the first size and a preset reference size includes:
determining the width of the first reference frame according to the width of a screen in the screen size and a preset first interval width;
determining the height of the first reference frame according to the width of the first reference frame, the first size, the screen size and a reference ratio of width to height in the reference sizes;
and taking the width of the first reference frame and the height of the first reference frame as the size of the first reference frame.
In one embodiment, the determining a second size according to the size of the first reference frame, the first size, and the screen size includes:
determining a width in the second size according to a width in the size of the first reference frame, a height in the first size, and a width in the screen size;
determining a height in the second size in accordance with a height in the size of the first reference frame, a width in the first size, and a height in the screen size.
In one embodiment, the acquiring the initial image includes:
and acquiring an image of the current shot object as the initial image.
In one embodiment, before the acquiring the current image of the photographed object as the initial image, the method further includes:
acquiring each preview size in a preset preview size list;
selecting a plurality of candidate preview sizes corresponding to the height of the preview size smaller than the height of the screen size and the width of the preview size smaller than the width of the screen size;
calculating a third ratio of width to height in each candidate preview size;
comparing each third ratio with a second ratio of width to height in the screen size to obtain a third ratio with the minimum difference value with the second ratio, and taking a candidate preview size corresponding to the third ratio as a third size;
determining the width of a second reference frame according to the width in the screen size and a preset second interval width;
determining the height of the second reference frame according to the width of the second reference frame, the third size, the screen size and a reference ratio of width to height in the reference sizes;
and drawing the second reference frame on a screen according to the width of the second reference frame and the height of the second reference frame.
In one embodiment, before the acquiring the current image of the photographed object as the initial image, the method further includes:
monitoring a layout change of a screen of the electronic device;
when the layout of the screen is monitored to be changed, a first value of the display state of the virtual key on the current screen and a second value of the display state of the virtual key on the screen before the layout is changed are obtained;
judging whether the first value is the same as the second value;
and if not, judging that the size of the screen of the electronic equipment is changed, returning to the step of acquiring the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size.
An apparatus for generating a photograph, the apparatus comprising:
the acquisition module is used for acquiring the screen size of the current electronic equipment and selecting a first size matched with the screen size from a preset image size list according to the screen size;
the first determining module is used for determining the size of the first reference frame according to the first size and a preset reference size;
the second determining module is used for determining a second size according to the size of the first reference frame, the first size and the screen size;
and the cutting module is used for obtaining an initial image and cutting the initial image according to the second size to obtain a target image file.
A computer device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of any of the above-described embodiments of the method when executing the computer program.
A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the steps of the method of any of the embodiments described above.
According to the image generation method, the image generation device, the computer equipment and the storage medium, the screen size of the current electronic equipment is obtained, and a first size matched with the screen size is selected from a preset image size list according to the screen size; determining the size of a first reference frame according to the first size and a preset reference size; determining a second size according to the size of the first reference frame, the first size and the screen size; and acquiring an initial image, and cutting the initial image according to the second size to obtain a target image file. The method and the device have the advantages that the initial image is cut based on the preset reference size, so that the cut image meets the preset size requirement, and meanwhile, the cutting of the image is more intelligent.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the present application.
The image generation method in the embodiment of the application can be applied to electronic equipment. The electronic device may be, but is not limited to, a mobile phone, a notebook, a tablet computer, and the like. Fig. 1 is a schematic diagram of an internal structure of an electronic device in an embodiment. The electronic device includes a processor, a memory, a network interface, a display screen, and an input device connected by a system bus. Wherein the processor is configured to provide computational and control capabilities. The memory includes a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and further includes an image generation apparatus for implementing an image generation method. The internal memory provides an environment for running the computer program in the nonvolatile storage medium. The display screen is used for displaying a target image generated by the current application program, and the display screen can be a liquid crystal display screen or an electronic ink display screen. The input device in the application can be an image acquisition device, such as a camera, a touch layer covered on a display screen, a key, a track ball or a touch pad arranged on an electronic equipment shell, and an external keyboard, a touch pad or a mouse.
In one embodiment, as shown in fig. 2, an image generation method is provided, which is described by taking the method as an example of the application program in fig. 1, and comprises the following steps:
s11, acquiring the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size.
In the present application, the current application program may be called by another application program that has previously accessed the application program. When the current application program receives a call instruction of other application programs, the image generation method can be executed.
In the present application, initialization is started when the current application is called. In the initialization process, the screen size of the current electronic equipment is obtained, and a first size matched with the screen size is selected from a preset image size list according to the screen size.
And S12, determining the size of the first reference frame according to the first size and a preset reference size.
In the application, the first reference frame is a virtual frame, and the second size for subsequent cutting is obtained through the first reference frame.
And S13, determining a second size according to the size of the first reference frame, the first size and the screen size.
In the present application, the second size may be determined by a width and a height in the size of the first reference frame, a width and a height in the first size, and a width and a height in the screen size. In the application, when the subsequent cropping is executed, the cropping is specifically executed on the initial image according to the second size.
And S14, acquiring the initial image, and cutting the initial image according to the second size to obtain the target image file.
In the application, the initial image is acquired after the second size is acquired, and the initial image is cut according to the second size. The method and the device can call the camera to acquire the image of the current shot object as the initial image when the initial image is acquired.
In another embodiment, the method and the device for generating the target image file can also obtain the initial image after obtaining the first size, determine the second size according to the size of the first reference frame, the first size and the screen size, and then cut the initial image according to the second size to obtain the target image file.
In one embodiment, the selecting a first size matching the screen size from the preset image size list according to the screen size includes:
acquiring each photo size in the image size list;
selecting a plurality of candidate photo sizes corresponding to the height of the photo size smaller than the height of the screen size and the width of the photo size smaller than the width of the screen size;
calculating a first ratio of width to height in each candidate photo size;
and comparing each first ratio with a second ratio of the width to the height in the screen size to obtain a first ratio with the minimum difference value with the second ratio, and taking the candidate photo size corresponding to the first ratio as the first size.
The present application sets in advance an image size list in which a plurality of photograph sizes are included. The application selects an optimal photo size matching the screen size from the plurality of photo sizes as the second size.
Specifically, if the picture size list includes a picture size that is consistent with the screen size, the picture size is directly acquired as a first size; if the picture size consistent with the screen size is not included in the image size list, a picture size consistent with or closest to the scale of the screen size is acquired as the first size. Preferably, in order to ensure memory usage, a photo size smaller than a preset size of a screen size may be selected as the first size.
In one embodiment, the determining the size of the first reference frame according to the first size and a preset reference size includes:
determining the width of a first reference frame according to the width of a screen in the screen size and a preset first interval width;
determining the height of the first reference frame according to the width of the first reference frame, the first size, the screen size and a reference ratio of the width to the height in the reference size;
the width of the first reference frame and the height of the first reference frame are taken as the size of the first reference frame.
Specifically, assuming that the width of the first reference frame is picFrameWidth, the height of the first reference frame is picFrameHeight, and the first interval width is M1, the specific formula for determining the width of the first reference frame according to the width of the screen in the screen size and the preset first interval width is as follows:
picFrameWidth=ScnWi dth-M1
further, the specific calculation formula for determining the height of the first reference frame according to the width of the first reference frame, the first size, the screen size and the reference ratio of the width to the height in the reference size is as follows:
picFrameHeight=picFrameWidth*ratio*picHeight/picWidth*ScnHeight/ScnWidth
wherein picHeight is the height in the first dimension, picWidth is the width in the first dimension, ScnHeight is the height of the screen, ScnWidth is the width of the screen, ratio is width/height, height is the height in the reference dimension, and width is the width in the reference dimension.
In one embodiment, the determining the second size according to the size of the first reference frame, the first size and the screen size includes:
determining a width in the second size according to a width in the size of the first reference frame, a height in the first size, and a width in the screen size;
the height in the second size is determined according to the height in the size of the first reference frame, the width in the first size, and the height in the screen size.
In this application, assuming that the height in the second size is realHeight and the width in the second size is realWidth, a specific calculation formula for determining the width in the second size according to the width in the size of the first reference frame, the height in the first size, and the width in the screen size is as follows:
realWidth=picFrameWidth*picHeight/ScnWidth
further, the height in the second size is determined according to the height in the size of the first reference frame, the width in the first size and the height in the screen size, and a specific calculation formula is as follows:
realHeight=picFrameHeight*picWidth/ScnHeight
wherein, realHeight is the height in the second size, picFrameHeight is the width of the first reference frame, picWidth is the width in the first size, and scnhight is the height of the screen.
In one embodiment, the acquiring the initial image includes: and acquiring an image of the current shot object as an initial image.
According to the method and the device, after the first size is obtained, shooting operation is executed, and a camera is called to collect the image of the current shot object to be used as an initial image.
In another embodiment, after the first size and the second size are obtained, shooting operation is executed, and a camera is called to acquire an image of a current shot object as an initial image.
In one embodiment, before the capturing the current image of the photographed object as the initial image, the method further includes:
acquiring each preview size in a preset preview size list;
selecting a plurality of candidate preview sizes corresponding to the height of the preview size smaller than the height of the screen size and the width of the preview size smaller than the width of the screen size;
calculating a third ratio of width to height in each candidate preview size;
comparing each third ratio with a second ratio of width to height in the screen size to obtain a third ratio with the minimum difference value with the second ratio, and taking the candidate preview size corresponding to the third ratio as a third size;
determining the width of a second reference frame according to the width in the screen size and a preset second interval width;
determining the height of the second reference frame according to the width of the second reference frame, the third size, the screen size and the reference ratio of the width to the height in the reference size;
and drawing the second reference frame on the screen according to the width of the second reference frame and the height of the second reference frame.
In this application, it is assumed that the width in the third dimension is preWidth, the height in the third dimension is preHeight, the width in the first dimension is picWidth, the height in the first dimension is picHeight, the width of the second reference frame is preFrameWidth, the height of the second reference frame is preFrameHeight, and the preset second interval width is M2. In the present application, a specific formula for determining the width of the second reference frame according to the width of the screen size and the preset second interval width is as follows:
preFrameWidth=ScnWidth-M2
further, a specific formula for determining the height of the second reference frame according to the width of the second reference frame, the third size, the screen size, and a reference ratio of the width to the height in the reference size is as follows:
preFrameHeight=preFrameWidth*ratio*preHeight/preWidth*ScnHeight/ScnWidth
wherein preFrameHeight is the height of the second base frame, preFrameWidth is the width of the second base frame, prefeedt is the height of the third size, preFrameWidth is the width of the third size, ScnHeight is the height of the screen, ScnWidth is the width of the screen, the reference ratio is width/height, height is the height of the reference size, and width is the width of the reference size.
In the application, the second reference frame is obtained, and the second reference frame is drawn on the screen, so that a user can conveniently place a current shot object such as an identity card according to the second reference frame when shooting. Specifically, the current shot object needs to be previewed in the shooting process of the user, taking the current shot object as an identity card as an example, it needs to be ensured that the screen of the electronic device is arranged in a preset distance right above the identity card during shooting, and meanwhile, the identity card is ensured to be in the second reference frame during previewing, and four sides of the identity card are respectively overlapped with four sides of the second reference frame.
Referring to fig. 3, fig. 3 is a screen diagram illustrating a second reference frame according to an embodiment. In fig. 3, the screen 2 on which the second reference frame is drawn includes the second reference frame 21 and the screen area 22, and the second reference frame 21 includes the width 211 of the second reference frame and the height 212 of the second reference frame.
In one embodiment, before the capturing the current image of the photographed object as the initial image, the method further includes:
monitoring a layout change of a screen of the electronic device;
when the layout of the screen is monitored to be changed, a first value of the display state of the virtual key on the current screen and a second value of the display state of the virtual key on the screen before the layout is changed are obtained;
judging whether the first value is the same as the second value;
if not, judging that the size of the screen of the electronic equipment is changed, returning to the step of obtaining the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size.
In this application, because electronic equipment is various, electronic equipment's screen size is also various. In the present Application, if the electronic device is a mobile phone, the vertical screen of the mobile phone is taken as a standard direction, that is, the default screen direction of the mobile phone, the shorter side is wide, and the longer side is high, and the width and the height of the screen of the mobile phone are obtained through an Application Programming Interface (API) provided by the system.
Furthermore, some electronic devices have a navigation bar inside the screen, and the navigation bar can also control the hidden and revealed state thereof through gestures at any time, and the height of the screen acquired in the application does not include the height of the navigation bar, so that when the navigation bar is in the displayed state, the height of the navigation bar needs to be removed to acquire the required height of the screen.
Further, the change of the screen size needs to be monitored before the initial image is acquired, and when the change of the screen size is monitored, the screen size needs to be obtained again, and other sizes, such as the size of the second reference frame and the size of the first reference frame, also need to be recalculated. In addition, because the screen size is not changed in a large range, when the screen size is monitored to be changed, the first size and the third size can be obtained by using the data obtained before, and the data do not need to be obtained again.
Referring to fig. 4, fig. 4 is a schematic diagram of a screen structure of an electronic device according to an embodiment. In fig. 4, the screen structure 3 of the electronic device includes a screen portion 31 and a navigation bar portion 32. The screen portion includes a width 311 of the screen and a height 312 of the screen. In the present application, the navigation bar portion 32 should be removed when calculating the screen size.
In one embodiment, as shown in fig. 5, there is provided an image processing apparatus including:
the acquisition module 11 is configured to acquire a screen size of the current electronic device, and select a first size matched with the screen size from a preset image size list according to the screen size;
a first determining module 12, configured to determine a size of the first reference frame according to the first size and a preset reference size;
a second determining module 13, configured to determine a second size according to the size of the first reference frame, the first size, and the screen size;
and the cutting module 14 is configured to obtain an initial image, and cut the initial image according to a second size to obtain a target image file.
In one embodiment, the obtaining module 11 includes:
an acquisition unit configured to acquire each photo size in the image size list;
selecting a plurality of candidate photo sizes corresponding to the height of the photo size smaller than the height of the screen size and the width of the photo size smaller than the width of the screen size;
calculating a first ratio of width to height in each candidate photo size;
and comparing each first ratio with a second ratio of the width to the height in the screen size to obtain a first ratio with the minimum difference value with the second ratio, and taking the candidate photo size corresponding to the first ratio as the first size.
In one embodiment, the first determining module 12 includes:
the first determining unit is used for determining the width of the first reference frame according to the width of the screen in the screen size and a preset first interval width;
determining the height of the first reference frame according to the width of the first reference frame, the first size, the screen size and a reference ratio of the width to the height in the reference size;
the width of the first reference frame and the height of the first reference frame are taken as the size of the first reference frame.
In one embodiment, the second determining module 13 includes:
a second determination unit for determining a width in the second size in accordance with a width in the size of the first reference frame, a height in the first size, and a width in the screen size;
the height in the second size is determined according to the height in the size of the first reference frame, the width in the first size, and the height in the screen size.
In one embodiment, the clipping module 14 includes:
and the acquisition unit is used for acquiring the image of the current shot object as an initial image.
In one embodiment, before the capturing the current image of the photographed object as the initial image, the method further includes:
acquiring each preview size in a preset preview size list;
selecting a plurality of candidate preview sizes corresponding to the height of the preview size smaller than the height of the screen size and the width of the preview size smaller than the width of the screen size;
calculating a third ratio of width to height in each candidate preview size;
comparing each third ratio with a second ratio of width to height in the screen size to obtain a third ratio with the minimum difference value with the second ratio, and taking the candidate preview size corresponding to the third ratio as a third size;
determining the width of a second reference frame according to the width in the screen size and a preset second interval width;
determining the height of the second reference frame according to the width of the second reference frame, the third size, the screen size and the reference ratio of the width to the height in the reference size;
and drawing the second reference frame on the screen according to the width of the second reference frame and the height of the second reference frame.
In one embodiment, before the capturing the current image of the photographed object as the initial image, the method further includes:
monitoring a layout change of a screen of the electronic device;
when the layout of the screen is monitored to be changed, a first value of the display state of the virtual key on the current screen and a second value of the display state of the virtual key on the screen before the layout is changed are obtained;
judging whether the first value is the same as the second value;
if not, judging that the size of the screen of the electronic equipment is changed, returning to the step of obtaining the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size.
In one embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be as shown in fig. 6. The computer device includes a processor, a memory, a network interface, a display screen, and an input device connected by a system bus. Wherein the processor of the computer device is configured to provide the determining and controlling capabilities. The memory of the computer device comprises a nonvolatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of an operating system and computer programs in the non-volatile storage medium. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to implement an image generation method. The display screen of the computer equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, a key, a track ball or a touch pad arranged on the shell of the computer equipment, an external keyboard, a touch pad or a mouse and the like.
In one embodiment, a computer device is provided, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, the processor implementing the following steps when executing the computer program:
acquiring the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size; determining the size of the first reference frame according to the first size and a preset reference size; determining a second size according to the size of the first reference frame, the first size and the screen size; and acquiring an initial image, and cutting the initial image according to a second size to obtain a target image file.
In one embodiment, the processor executes the computer program to select the first size matching the screen size from the preset image size list according to the screen size, and specifically implements the following steps:
acquiring each photo size in the image size list;
selecting a plurality of candidate photo sizes corresponding to the height of the photo size smaller than the height of the screen size and the width of the photo size smaller than the width of the screen size;
calculating a first ratio of width to height in each candidate photo size;
and comparing each first ratio with a second ratio of the width to the height in the screen size to obtain a first ratio with the minimum difference value with the second ratio, and taking the candidate photo size corresponding to the first ratio as the first size.
In an embodiment, the processor executes the computer program to determine the size of the first reference frame according to the first size and the preset reference size, and specifically implements the following steps:
determining the width of a first reference frame according to the width of a screen in the screen size and a preset first interval width;
determining the height of the first reference frame according to the width of the first reference frame, the first size, the screen size and a reference ratio of the width to the height in the reference size;
the width of the first reference frame and the height of the first reference frame are taken as the size of the first reference frame.
In an embodiment, the processor executes the computer program to determine the second size according to the size of the first reference frame, the first size, and the screen size, and specifically implements the following steps:
determining a width in the second size according to a width in the size of the first reference frame, a height in the first size, and a width in the screen size;
the height in the second size is determined according to the height in the size of the first reference frame, the width in the first size, and the height in the screen size.
In one embodiment, the processor executes the computer program to obtain the initial image, and specifically implements the following steps:
and acquiring an image of the current shot object as an initial image.
In an embodiment, before the processor executes the computer program to acquire the current image of the photographed object as the initial image, the following steps are specifically implemented:
acquiring each preview size in a preset preview size list;
selecting a plurality of candidate preview sizes corresponding to the height of the preview size smaller than the height of the screen size and the width of the preview size smaller than the width of the screen size;
calculating a third ratio of width to height in each candidate preview size;
comparing each third ratio with a second ratio of width to height in the screen size to obtain a third ratio with the minimum difference value with the second ratio, and taking the candidate preview size corresponding to the third ratio as a third size;
determining the width of a second reference frame according to the width in the screen size and a preset second interval width;
determining the height of the second reference frame according to the width of the second reference frame, the third size, the screen size and the reference ratio of the width to the height in the reference size;
and drawing the second reference frame on the screen according to the width of the second reference frame and the height of the second reference frame.
In an embodiment, before the processor executes the computer program to acquire the current image of the photographed object as the initial image, the following steps are specifically implemented:
monitoring a layout change of a screen of the electronic device;
when the layout of the screen is monitored to be changed, a first value of the display state of the virtual key on the current screen and a second value of the display state of the virtual key on the screen before the layout is changed are obtained;
judging whether the first value is the same as the second value;
if not, judging that the size of the screen of the electronic equipment is changed, returning to the step of obtaining the screen size of the current electronic equipment, and selecting a first size matched with the screen size from a preset image size list according to the screen size.
It will be understood by those skilled in the art that all or part of the processes of the methods of the embodiments described above can be implemented by hardware related to instructions of a computer program, which can be stored in a non-volatile computer-readable storage medium, and when executed, can include the processes of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the embodiments provided herein may include non-volatile and/or volatile memory, among others. Non-volatile memory can include read-only memory (ROM), Programmable ROM (PROM), Electrically Programmable ROM (EPROM), Electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced SDRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), Rambus Direct RAM (RDRAM), direct bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).
The technical features of the above embodiments can be arbitrarily combined, and for the sake of brevity, all possible combinations of the technical features in the above embodiments are not described, but should be considered as the scope of the present specification as long as there is no contradiction between the combinations of the technical features.
The above examples only express several embodiments of the present application, and the description thereof is more specific and detailed, but not construed as limiting the scope of the invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the concept of the present application, which falls within the scope of protection of the present application. Therefore, the protection scope of the present patent shall be subject to the appended claims.