An electronic device with a pattern detection function, comprising: an image sensor, a light source, and a cover plate and a light guide plate that are sequentially disposed in a stacked manner, wherein the light source is configured to emit emitted light; the light guide plate comprises one or more grating groups, and each grating group comprises a first grating part, a second grating part, and a third grating part; the first grating part is configured to: receive the emitted light through a light incident surface of the first grating part, diffract the emitted light to form first light, and enable the first light to enter the second grating part; the second grating part is configured to: form, based on the first light, second light propagated in the second grating part, and form, based on the second light, third light entering the third grating part; the third grating part is configured to: form, based on the third light, fourth light propagated in the third grating part, form emergent light based on the fourth light, and enable the emergent light to enter the cover plate after the emergent light is emitted from a light emergent surface of the third grating part; and the image sensor is configured to receive reflected light that passes through the light guide plate, to form image data of a to-be-detected pattern, wherein the reflected light is light formed by totally reflecting the emergent light by a top surface, away from the light guide plate, of the cover plate; or the reflected light is light formed by reflecting the emergent light by the to-be-detected pattern; wherein the electronic device further comprises a filtering layer, the filtering layer is located on a side, facing the second bonding layer, of the image sensor and is bonded to the image sensor, and a refractive index of the filtering layer is greater than or equal to 1.3; and the filtering layer is an optical filter, light transmittance of the optical filter for light in a selected band is higher than light transmittance of the optical filter for a light outside the selected band, and the selected band ranges from (λ - 5) nm to (λ + 5) nm, wherein λ is a wavelength of the emitted light; or the filtering layer is an optical coated sheet, light transmittance of the optical coated sheet for incident light within a specific incident-angle range is higher than light transmittance of incident light outside the specific incident-angle range, and the specific incident-angle range is γ to 90°, wherein γ is an emergence angle at which the emergent light is emitted from the third grating; wherein the to-be-detected pattern comprises: a fingerprint, a palm print, or a touch action. the light source is configured to emit emitted light; the light guide plate comprises one or more grating groups, and each grating group comprises a first grating part, a second grating part, and a third grating part; the first grating part is configured to: receive the emitted light through a light incident surface of the first grating part, diffract the emitted light to form first light, and enable the first light to enter the second grating part; the second grating part is configured to: form, based on the first light, second light propagated in the second grating part, and form, based on the second light, third light entering the third grating part; the third grating part is configured to: form, based on the third light, fourth light propagated in the third grating part, form emergent light based on the fourth light, and enable the emergent light to enter the cover plate after the emergent light is emitted from a light emergent surface of the third grating part; and the image sensor is configured to receive reflected light that passes through the light guide plate, to form image data of a to-be-detected pattern, wherein the reflected light is light formed by totally reflecting the emergent light by a top surface, away from the light guide plate, of the cover plate; or the reflected light is light formed by reflecting the emergent light by the to-be-detected pattern; wherein the electronic device further comprises a filtering layer, the filtering layer is located on a side, facing the second bonding layer, of the image sensor and is bonded to the image sensor, and a refractive index of the filtering layer is greater than or equal to 1.3; and the filtering layer is an optical filter, light transmittance of the optical filter for light in a selected band is higher than light transmittance of the optical filter for a light outside the selected band, and the selected band ranges from (λ - 5) nm to (λ + 5) nm, wherein λ is a wavelength of the emitted light; or the filtering layer is an optical coated sheet, light transmittance of the optical coated sheet for incident light within a specific incident-angle range is higher than light transmittance of incident light outside the specific incident-angle range, and the specific incident-angle range is γ to 90°, wherein γ is an emergence angle at which the emergent light is emitted from the third grating; wherein the to-be-detected pattern comprises: a fingerprint, a palm print, or a touch action.