MonkeyImage
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Classe monkeyrunner qui contient une image de l'écran de l'appareil ou de l'émulateur. L'image est copiée depuis la mémoire tampon écran lors d'une capture d'écran. Les méthodes de cet objet vous permettent de convertir l'image dans différents formats de stockage, d'écrire l'image dans un fichier, de copier des parties de l'image et de comparer cet objet à d'autres objets MonkeyImage
.
Vous n'avez pas besoin de créer de nouvelles instances de MonkeyImage
. Utilisez plutôt
MonkeyDevice.takeSnapshot()
pour créer une nouvelle instance à partir d'une capture d'écran. Par exemple, utilisez :
newimage = MonkeyDevice.takeSnapshot()
Résumé
Méthodes |
string
|
convertToBytes
(format de string)
Convertit l'image actuelle dans un format particulier et la renvoie sous forme de string à laquelle vous pouvez ensuite accéder en tant que iterable d'octets binaires.
|
tuple
|
getRawPixel
(integer x,
integer y)
Renvoie le pixel unique à l'emplacement de l'image (x,y), en tant que tuple de integer, au format (a,r,g,b).
|
integer
|
getRawPixelInt
(integer x,
integer y)
Renvoie le pixel unique à l'emplacement de l'image (x,y), en tant que integer 32 bits.
|
MonkeyImage
|
getSubImage
(tuple rect)
Crée un nouvel objet MonkeyImage à partir d'une sélection rectangulaire de l'image actuelle.
|
boolean
|
sameAs
(autre MonkeyImage ,
pourcentage de float)
Compare cet objet MonkeyImage à un autre et renvoie le résultat de la comparaison. L'argument percent spécifie la différence de pourcentage autorisée pour que les deux images soient "égales".
|
void
|
writeToFile
(chemin d'accès de string,
format de string)
Écrit l'image actuelle dans le fichier spécifié par filename , au format spécifié par format .
|
Méthodes publiques
Convertit l'image actuelle dans un format particulier et la renvoie sous forme de string à laquelle vous pouvez ensuite accéder en tant que iterable d'octets binaires.
Arguments
format |
Format de sortie souhaité. Tous les formats de sortie de trame courants sont pris en charge.
La valeur par défaut est "png" (Portable Network Graphics).
|
tuple
getRawPixel
(integer x,
integer y)
Renvoie le pixel unique à l'emplacement de l'image (x,y), en tant que tuple de integer, au format (a,r,g,b).
Arguments
x |
Position horizontale du pixel, commençant par 0 à gauche de l'écran dans l'orientation définie au moment de la capture d'écran.
|
y |
Position verticale du pixel, commençant par 0 en haut de l'écran dans l'orientation définie au moment de la capture d'écran.
|
Renvoie
-
Un tuple d'entiers représentant le pixel, au format (a,r,g,b), où "a" est la valeur du canal alpha, et "r", "g" et "b" sont les valeurs rouge, vert et bleu, respectivement.
tuple
getRawPixelInt
(integer x,
integer y)
Renvoie le pixel unique à l'emplacement de l'image (x,y), en tant que integer. Utilisez cette méthode pour économiser de la mémoire.
Arguments
x |
Position horizontale du pixel, commençant par 0 à gauche de l'écran dans l'orientation définie au moment de la capture d'écran.
|
y |
Position verticale du pixel, commençant par 0 en haut de l'écran dans l'orientation définie au moment de la capture d'écran.
|
Renvoie
-
Les valeurs a, r, g et b du pixel sous la forme de valeurs 8 bits combinées en un entier 32 bits, "a" correspondant aux 8 bits les plus à gauche, "r" aux 8 bits les plus à droite, etc.
MonkeyImage
getSubImage
(tuple rect)
Crée un nouvel objet MonkeyImage
à partir d'une sélection rectangulaire de l'image actuelle.
Arguments
rect |
Un tuple (x, y, w, h) spécifiant la sélection. "x" et "y" indiquent la position du pixel en base 0 dans l'angle supérieur gauche de la sélection. "w" spécifie la largeur de la région, et "h" spécifie sa hauteur, toutes deux en unités de pixels.
L'orientation de l'image est identique à l'orientation de l'écran au moment de la capture d'écran.
|
Renvoie
-
Un nouvel objet
MonkeyImage
contenant la sélection
boolean
sameAs
(
MonkeyImage
otherImage,
pourcentage de float
)
Compare cet objet MonkeyImage
à un autre et renvoie le résultat de la comparaison. L'argument percent
spécifie la différence en pourcentage autorisée pour que les deux images soient "égales".
Arguments
other |
Un autre objet MonkeyImage à comparer à celui-ci.
|
percent
|
Valeur flottante comprise entre 0,0 et 1,0 (inclus), indiquant le pourcentage de pixels devant être identique pour que la méthode renvoie true . La valeur par défaut est 1,0. Elle indique que tous les pixels doivent correspondre.
|
Renvoie
-
Booléen
true
si les images correspondent, booléen false
dans le cas contraire.
Écrit l'image actuelle dans le fichier spécifié par filename
, au format spécifié par format
.
Arguments
path |
Nom de fichier complet et extension du fichier de sortie.
|
format
|
Format de sortie à utiliser pour le fichier. Si aucun format n'est fourni, la méthode tente de deviner le format à partir de l'extension du nom de fichier. Si aucune extension n'est fournie et qu'aucun format n'est spécifié, le format par défaut "png" (Portable Network Graphics) est utilisé.
|
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 2025/07/27 (UTC)."],[],[],null,["# MonkeyImage\n\nA monkeyrunner class to hold an image of the device or emulator's screen. The image is\ncopied from the screen buffer during a screenshot. This object's methods allow you to\nconvert the image into various storage formats, write the image to a file, copy parts of\nthe image, and compare this object to other `MonkeyImage` objects.\n\n\nYou do not need to create new instances of `MonkeyImage`. Instead, use\n[MonkeyDevice.takeSnapshot()](/tools/help/MonkeyDevice#takeSnapshot) to create a new instance from a screenshot. For example, use: \n\n```\nnewimage = MonkeyDevice.takeSnapshot()\n```\n\nSummary\n-------\n\n| Methods ||||||||||||\n|----------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|---|---|---|---|---|---|---|---|---|\n| *string* | [convertToBytes](#convertToBytes) (*string* format) Converts the current image to a particular format and returns it as a *string* that you can then access as an *iterable* of binary bytes. |\n| *tuple* | [getRawPixel](#getRawPixel) (*integer* x, *integer* y) Returns the single pixel at the image location (x,y), as an a *tuple* of *integer*, in the form (a,r,g,b). |\n| *integer* | [getRawPixelInt](#getRawPixelInt) (*integer* x, *integer* y) Returns the single pixel at the image location (x,y), as a 32-bit *integer*. |\n| ` `[MonkeyImage](/tools/help/MonkeyImage)` ` | [getSubImage](#getSubImage) (*tuple* rect) Creates a new `MonkeyImage` object from a rectangular selection of the current image. |\n| *boolean* | [sameAs](#sameAs) ([MonkeyImage](/tools/help/MonkeyImage) other, *float* percent) Compares this `MonkeyImage` object to another and returns the result of the comparison. The `percent` argument specifies the percentage difference that is allowed for the two images to be \"equal\". |\n| *void* | [writeToFile](#writeToFile) (*string* path, *string* format) Writes the current image to the file specified by `filename`, in the format specified by `format`. |\n\nPublic methods\n--------------\n\n#### *string*\nconvertToBytes\n( *string* format)\n\n\nConverts the current image to a particular format and returns it as a *string*\nthat you can then access as an *iterable* of binary bytes. \n\n##### Arguments\n\n| format | The desired output format. All of the common raster output formats are supported. The default value is \"png\" (Portable Network Graphics). |\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------|\n\n#### *tuple*\ngetRawPixel\n(*integer* x, *integer* y)\n\n\nReturns the single pixel at the image location (x,y), as an\na *tuple* of *integer*, in the form (a,r,g,b). \n\n##### Arguments\n\n| x | The horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken. |\n| y | The vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken. |\n|---|------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A tuple of integers representing the pixel, in the form (a,r,g,b) where a is the alpha channel value, and r, g, and b are the red, green, and blue values, respectively. \n\n#### *tuple*\ngetRawPixelInt\n(*integer* x, *integer* y)\n\n\nReturns the single pixel at the image location (x,y), as an\nan *integer*. Use this method to economize on memory. \n\n##### Arguments\n\n| x | The horizontal position of the pixel, starting with 0 at the left of the screen in the orientation it had when the screenshot was taken. |\n| y | The vertical position of the pixel, starting with 0 at the top of the screen in the orientation it had when the screenshot was taken. |\n|---|------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- The a,r,g, and b values of the pixel as 8-bit values combined into a 32-bit integer, with a as the leftmost 8 bits, r the next rightmost, and so forth. \n\n#### `\n`[MonkeyImage](/tools/help/MonkeyImage)`\n`\ngetSubImage\n(*tuple* rect)\n\n\nCreates a new `MonkeyImage` object from a rectangular selection of the\ncurrent image. \n\n##### Arguments\n\n| rect | A tuple (x, y, w, h) specifying the selection. x and y specify the 0-based pixel position of the upper left-hand corner of the selection. w specifies the width of the region, and h specifies its height, both in units of pixels. The image's orientation is the same as the screen orientation at the time the screenshot was made. |\n|------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- A new `MonkeyImage` object containing the selection. \n\n#### *boolean*\nsameAs\n( `\n`[MonkeyImage](/tools/help/MonkeyImage)`\n` otherImage, *float* percent )\n\n\nCompares this `MonkeyImage` object to another and returns the result of\nthe comparison. The `percent` argument specifies the percentage\ndifference that is allowed for the two images to be \"equal\". \n\n##### Arguments\n\n| other | Another `MonkeyImage` object to compare to this one. |\n| percent | A float in the range 0.0 to 1.0, inclusive, indicating the percentage of pixels that need to be the same for the method to return `true`. The default is 1.0, indicating that all the pixels must match. |\n|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n\n##### Returns\n\n- Boolean `true` if the images match, or boolean `false` otherwise. \n\n#### void\nwriteToFile\n(*string* filename, *string* format)\n\n\nWrites the current image to the file specified by `filename`, in the\nformat specified by `format`. \n\n##### Arguments\n\n| path | The fully-qualified filename and extension of the output file. |\n| format | The output format to use for the file. If no format is provided, then the method tries to guess the format from the filename's extension. If no extension is provided and no format is specified, then the default format of \"png\" (Portable Network Graphics) is used. |\n|--------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|"]]