{"id":1340,"date":"2010-06-21T13:30:21","date_gmt":"2010-06-21T11:30:21","guid":{"rendered":"http:\/\/blogs.wittwer.fr\/whiler\/?p=1340"},"modified":"2023-03-12T15:03:28","modified_gmt":"2023-03-12T14:03:28","slug":"qr_code","status":"publish","type":"post","link":"https:\/\/blogs.wittwer.fr\/whiler\/2010\/06\/21\/qr_code\/","title":{"rendered":"Cr\u00e9er des codes QR dynamiques dans une application Android"},"content":{"rendered":"<p>On voit de plus en plus des images comme celle-ci&#8230;<br \/>\n<center><a href=\"https:\/\/blogs.wittwer.fr\/whiler\/2010\/06\/21\/qr_code\/\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/plugins\/qr-code-tag\/data\/qrct-7d729ca3d56e4327a89c12f7fa72cbf8.png\" class=\"qrctimage\" \/><\/a><\/center><\/p>\n<p>&#8230; les <a target=\"_blank\" href=\"https:\/\/www.android-software.fr\/tout-ce-que-vous-avez-toujours-voulu-savoir-sur-le-qr-code-sans-jamais-oser-le-demander\" rel=\"noopener\">codes QR<\/a>&nbsp;!<\/p>\n<p>Pour en g\u00e9n\u00e9rer, il existe de nombreuses solutions sur <a target=\"_blank\" href=\"https:\/\/tinyurl.com\/36cbstr\" rel=\"noopener\">internet<\/a>&#8230;<\/p>\n<p>Afin d&rsquo;optimiser ses applications <a href=\"http:\/\/fr.wikipedia.org\/wiki\/Android\" rel=\"glossary\" target=\"_blank\" title=\"Wikipedia, D&eacute;finition de&nbsp;: Android\" style=\"\" >Android<\/a><sup style=\"font-family: Georgia, Times New Roman, Serif; font-weight: bold; color: #AAAAAA\" ><em>W<\/em><\/sup>, il vaudrait mieux avoir des images statiques afin de ne pas consommer de CPU sur les p\u00e9riph\u00e9riques o\u00f9 l&rsquo;application est d\u00e9ploy\u00e9e&#8230; Cependant, on peut parfois avoir besoin d&rsquo;en g\u00e9n\u00e9rer de fa\u00e7on dynamique&#8230;<\/p>\n<p>Je vous propose dans cet article, une solution assez simple qui fonctionne pour ajouter cette fonctionnalit\u00e9 \u00e0 vos applications.<br \/>\n<!--more--><\/p>\n<p>Pour l&rsquo;impl\u00e9menter, j&rsquo;ai t\u00e9l\u00e9charg\u00e9 la solution Java de <a target=\"_blank\" href=\"https:\/\/code.google.com\/p\/d-project\/\" rel=\"noopener\">Kazuhiko Arase<\/a>, et je l&rsquo;ai l\u00e9g\u00e8rement modifi\u00e9e pour qu&rsquo;elle fonctionne avec le <a target=\"_blank\" href=\"https:\/\/developer.android.com\/intl\/fr\/sdk\/\" rel=\"noopener\">SDK d&rsquo;Android<\/a> :<br \/>\nEntre autre, la fa\u00e7on de <a target=\"_blank\" href=\"https:\/\/developer.android.com\/intl\/fr\/reference\/android\/graphics\/Bitmap.html#setPixel%28int,%20int,%20int%29\" rel=\"noopener\">modifier les pixels<\/a> d&rsquo;<a target=\"_blank\" href=\"https:\/\/developer.android.com\/intl\/fr\/reference\/android\/graphics\/Bitmap.html#createBitmap%28android.graphics.Bitmap%29\" rel=\"noopener\">une image en m\u00e9moire<\/a>&#8230;<\/p>\n<p>Avant :<\/p>\n<div class=\"codecolorer-container java dawn\" style=\"overflow:auto;white-space:nowrap;width:480px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/><\/div><\/td><td><div class=\"java codecolorer\">&nbsp; &nbsp; <span class=\"kw1\">public<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedimage\"><span class=\"kw3\">BufferedImage<\/span><\/a> createImage<span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> cellSize, <span class=\"kw4\">int<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"kw1\">throws<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception\"><span class=\"kw3\">IOException<\/span><\/a> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> imageSize <span class=\"sy0\">=<\/span> getModuleCount<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> cellSize <span class=\"sy0\">+<\/span> margin <span class=\"sy0\">*<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedimage\"><span class=\"kw3\">BufferedImage<\/span><\/a> image <span class=\"sy0\">=<\/span> <span class=\"kw1\">new<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedimage\"><span class=\"kw3\">BufferedImage<\/span><\/a><span class=\"br0\">&#40;<\/span>imageSize, imageSize, <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+bufferedimage\"><span class=\"kw3\">BufferedImage<\/span><\/a>.<span class=\"me1\">TYPE_INT_RGB<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> y <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> y <span class=\"sy0\">&lt;<\/span> imageSize<span class=\"sy0\">;<\/span> y<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> x <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> x <span class=\"sy0\">&lt;<\/span> imageSize<span class=\"sy0\">;<\/span> x<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>margin <span class=\"sy0\">&lt;=<\/span> x <span class=\"sy0\">&amp;&amp;<\/span> x <span class=\"sy0\">&lt;<\/span> imageSize <span class=\"sy0\">-<\/span> margin<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> margin <span class=\"sy0\">&lt;=<\/span> y <span class=\"sy0\">&amp;&amp;<\/span> y <span class=\"sy0\">&lt;<\/span> imageSize <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> col <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span>x <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">\/<\/span> cellSize<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> row <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span>y <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">\/<\/span> cellSize<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>isDark<span class=\"br0\">&#40;<\/span>row, col<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setRGB<\/span><span class=\"br0\">&#40;<\/span>x, y, 0x000000<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setRGB<\/span><span class=\"br0\">&#40;<\/span>x, y, 0xffffff<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setRGB<\/span><span class=\"br0\">&#40;<\/span>x, y, 0xffffff<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> image<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Apr\u00e8s :<\/p>\n<div class=\"codecolorer-container java dawn\" style=\"overflow:auto;white-space:nowrap;width:480px;height:300px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/><\/div><\/td><td><div class=\"java codecolorer\">&nbsp; &nbsp; <span class=\"kw1\">public<\/span> Bitmap createImage<span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> cellSize, <span class=\"kw4\">int<\/span> margin, <span class=\"kw4\">int<\/span> cForeground, <span class=\"kw4\">int<\/span> cBackground, <span class=\"kw4\">int<\/span> cMargin<span class=\"br0\">&#41;<\/span> <span class=\"kw1\">throws<\/span> <a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+ioexception\"><span class=\"kw3\">IOException<\/span><\/a> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> imageSize <span class=\"sy0\">=<\/span> getModuleCount<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">*<\/span> cellSize <span class=\"sy0\">+<\/span> margin <span class=\"sy0\">*<\/span> <span class=\"nu0\">2<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; Bitmap image <span class=\"sy0\">=<\/span> Bitmap.<span class=\"me1\">createBitmap<\/span><span class=\"br0\">&#40;<\/span>imageSize, imageSize, Bitmap.<span class=\"me1\">Config<\/span>.<span class=\"me1\">ARGB_8888<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> y <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> y <span class=\"sy0\">&lt;<\/span> imageSize<span class=\"sy0\">;<\/span> y<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">for<\/span> <span class=\"br0\">&#40;<\/span><span class=\"kw4\">int<\/span> x <span class=\"sy0\">=<\/span> <span class=\"nu0\">0<\/span><span class=\"sy0\">;<\/span> x <span class=\"sy0\">&lt;<\/span> imageSize<span class=\"sy0\">;<\/span> x<span class=\"sy0\">++<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>margin <span class=\"sy0\">&lt;=<\/span> x <span class=\"sy0\">&amp;&amp;<\/span> x <span class=\"sy0\">&lt;<\/span> imageSize <span class=\"sy0\">-<\/span> margin<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> margin <span class=\"sy0\">&lt;=<\/span> y <span class=\"sy0\">&amp;&amp;<\/span> y <span class=\"sy0\">&lt;<\/span> imageSize <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> col <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span>x <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">\/<\/span> cellSize<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span> row <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span>y <span class=\"sy0\">-<\/span> margin<span class=\"br0\">&#41;<\/span> <span class=\"sy0\">\/<\/span> cellSize<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>isDark<span class=\"br0\">&#40;<\/span>row, col<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setPixel<\/span><span class=\"br0\">&#40;<\/span>x, y, cForeground<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setPixel<\/span><span class=\"br0\">&#40;<\/span>x, y, cBackground<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; image.<span class=\"me1\">setPixel<\/span><span class=\"br0\">&#40;<\/span>x, y, cMargin<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">return<\/span> image<span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Vous pouvez t\u00e9l\u00e9charger le code source depuis ce lien : <a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/qrcode.zip\">Code source<\/a><\/p>\n<p>Sur la page suivante, je vais vous montrer comment l&rsquo;utiliser pour faire une impl\u00e9mentation des plus simples&#8230;<br \/>\n<!--nextpage--><br \/>\nJe commence par cr\u00e9er un nouveau projet :<br \/>\n<div id=\"attachment_1346\" style=\"width: 213px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/new_project.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1346\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/new_project-203x300.png\" alt=\"Nouveau projet Android\" title=\"Nouveau projet Android\" width=\"203\" height=\"300\" class=\"size-medium wp-image-1346\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/new_project-203x300.png 203w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/new_project.png 500w\" sizes=\"auto, (max-width: 203px) 100vw, 203px\" \/><\/a><p id=\"caption-attachment-1346\" class=\"wp-caption-text\">Nouveau projet Android<\/p><\/div><br \/>\nPuis j&rsquo;importe le code source du <a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/qrcode.zip\">zip<\/a> dans ce projet :<br \/>\n<div id=\"attachment_1347\" style=\"width: 266px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/import_archive.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1347\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/import_archive-256x300.png\" alt=\"Importer une archive\" title=\"Importer une archive\" width=\"256\" height=\"300\" class=\"size-medium wp-image-1347\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/import_archive-256x300.png 256w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/import_archive.png 470w\" sizes=\"auto, (max-width: 256px) 100vw, 256px\" \/><\/a><p id=\"caption-attachment-1347\" class=\"wp-caption-text\">Importer une archive<\/p><\/div><br \/>\n<div id=\"attachment_1348\" style=\"width: 306px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/archive_imported.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1348\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/archive_imported-296x300.png\" alt=\"Archive import\u00e9e\" title=\"Archive import\u00e9e\" width=\"296\" height=\"300\" class=\"size-medium wp-image-1348\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/archive_imported-296x300.png 296w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/archive_imported.png 543w\" sizes=\"auto, (max-width: 296px) 100vw, 296px\" \/><\/a><p id=\"caption-attachment-1348\" class=\"wp-caption-text\">Archive import\u00e9e<\/p><\/div><br \/>\nLe projet contient ainsi les fichiers n\u00e9cessaires \u00e0 la g\u00e9n\u00e9ration des codes QR. Je vous invite \u00e0 lire le fichier readme.txt concernant les licences associ\u00e9es.<br \/>\n<div id=\"attachment_1352\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/project_ready.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1352\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/project_ready-300x216.png\" alt=\"Le projet est pr\u00eat\" title=\"Le projet est pr\u00eat\" width=\"300\" height=\"216\" class=\"size-medium wp-image-1352\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/project_ready-300x216.png 300w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/project_ready.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1352\" class=\"wp-caption-text\">Le projet est pr\u00eat<\/p><\/div><br \/>\nJ&rsquo;ajoute un composant dans la mise en page de l&rsquo;application g\u00e9n\u00e9r\u00e9e par d\u00e9faut qui affichera mon code QR :<br \/>\n<div id=\"attachment_1349\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/add_imagebutton.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1349\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/add_imagebutton-300x216.png\" alt=\"Ajout d&#039;un ImageButton\" title=\"Ajout d&#039;un ImageButton\" width=\"300\" height=\"216\" class=\"size-medium wp-image-1349\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/add_imagebutton-300x216.png 300w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/add_imagebutton.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1349\" class=\"wp-caption-text\">Ajout d'un ImageButton<\/p><\/div><br \/>\nJ&rsquo;ajoute la cha\u00eene de caract\u00e8res qui contient le texte \u00e0 afficher dans le code QR, dans le cas pr\u00e9sent, une adresse Web :<br \/>\n<div id=\"attachment_1350\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/string_resource.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1350\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/string_resource-300x216.png\" alt=\"Ajout d&#039;une ressource textuelle\" title=\"Ajout d&#039;une ressource textuelle\" width=\"300\" height=\"216\" class=\"size-medium wp-image-1350\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/string_resource-300x216.png 300w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/string_resource.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1350\" class=\"wp-caption-text\">Ajout d'une ressource textuelle<\/p><\/div><br \/>\nTout est pr\u00eat, il suffit d&rsquo;appeler les m\u00e9thodes n\u00e9cessaires pour g\u00e9n\u00e9rer et afficher le code QR dans le composant pr\u00e9c\u00e9demment ajout\u00e9 :<br \/>\n<div id=\"attachment_1353\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/source_code.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1353\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/source_code-300x216.png\" alt=\"Code source\" title=\"Code source\" width=\"300\" height=\"216\" class=\"size-medium wp-image-1353\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/source_code-300x216.png 300w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/source_code.png 1024w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1353\" class=\"wp-caption-text\">Code source<\/p><\/div><\/p>\n<div class=\"codecolorer-container java dawn\" style=\"overflow:auto;white-space:nowrap;width:480px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/><\/div><\/td><td><div class=\"java codecolorer\">&nbsp; &nbsp; <span class=\"kw1\">public<\/span> <span class=\"kw4\">void<\/span> onCreate<span class=\"br0\">&#40;<\/span>Bundle savedInstanceState<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">super<\/span>.<span class=\"me1\">onCreate<\/span><span class=\"br0\">&#40;<\/span>savedInstanceState<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; setContentView<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">layout<\/span>.<span class=\"me1\">main<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; imgBtnQR <span class=\"sy0\">=<\/span> <span class=\"br0\">&#40;<\/span>ImageButton<span class=\"br0\">&#41;<\/span> findViewById<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">id<\/span>.<span class=\"me1\">ImgBtnLink<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">try<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ G\u00e9n\u00e8re les donn\u00e9es en fonction de ma ressource<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; QRCode qrc <span class=\"sy0\">=<\/span> QRCode.<span class=\"me1\">getMinimumQRCode<\/span><span class=\"br0\">&#40;<\/span>getString<span class=\"br0\">&#40;<\/span>R.<span class=\"me1\">string<\/span>.<span class=\"me1\">MonLien<\/span><span class=\"br0\">&#41;<\/span>, ErrorCorrectLevel.<span class=\"me1\">L<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ D\u00e9finit la taille<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw4\">int<\/span><span class=\"br0\">&#91;<\/span><span class=\"br0\">&#93;<\/span> iSizeMargin <span class=\"sy0\">=<\/span> <span class=\"br0\">&#123;<\/span><span class=\"nu0\">4<\/span>, <span class=\"nu0\">4<\/span><span class=\"br0\">&#125;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ G\u00e9n\u00e8re l'image du code QR<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bmp <span class=\"sy0\">=<\/span> qrc.<span class=\"me1\">createImage<\/span><span class=\"br0\">&#40;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; iSizeMargin<span class=\"br0\">&#91;<\/span><span class=\"nu0\">0<\/span><span class=\"br0\">&#93;<\/span>, iSizeMargin<span class=\"br0\">&#91;<\/span><span class=\"nu0\">1<\/span><span class=\"br0\">&#93;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getResources<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">getColor<\/span><span class=\"br0\">&#40;<\/span>android.<span class=\"me1\">R<\/span>.<span class=\"me1\">color<\/span>.<span class=\"me1\">black<\/span><span class=\"br0\">&#41;<\/span>,<br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getResources<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>.<span class=\"me1\">getColor<\/span><span class=\"br0\">&#40;<\/span>android.<span class=\"me1\">R<\/span>.<span class=\"me1\">color<\/span>.<span class=\"me1\">white<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"co1\">\/\/ D\u00e9finit l'image dans la mise en page<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; imgBtnQR.<span class=\"me1\">setImageBitmap<\/span><span class=\"br0\">&#40;<\/span>bmp<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">catch<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.google.com\/search?hl=en&amp;q=allinurl%3Adocs.oracle.com+javase+docs+api+exception\"><span class=\"kw3\">Exception<\/span><\/a> e<span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.<span class=\"me1\">w<\/span><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;QRCodeError&quot;<\/span>, e<span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> &nbsp; <br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Il ne reste plus qu&rsquo;\u00e0 lancer l&rsquo;\u00e9mulateur pour v\u00e9rifier le r\u00e9sultat :<br \/>\n<div id=\"attachment_1354\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"\/whiler\/wp-content\/uploads\/2010\/06\/test_emulator.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1354\" src=\"\/whiler\/wp-content\/uploads\/2010\/06\/test_emulator-300x210.png\" alt=\"Test dans l&#039;\u00e9mulateur\" title=\"Test dans l&#039;\u00e9mulateur\" width=\"300\" height=\"210\" class=\"size-medium wp-image-1354\" srcset=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/test_emulator-300x210.png 300w, https:\/\/blogs.wittwer.fr\/whiler\/wp-content\/uploads\/2010\/06\/test_emulator.png 797w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1354\" class=\"wp-caption-text\">Test dans l'\u00e9mulateur<\/p><\/div><\/p>\n<p>Le code QR a \u00e9t\u00e9 effectivement\/correctement g\u00e9n\u00e9r\u00e9&nbsp;! <img src=\"https:\/\/blogs.wittwer.fr\/whiler\/wp-includes\/images\/smilies\/skype\/\/yes.gif\" alt=\"(y)\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>On voit de plus en plus de codes QR !<\/p>\n<p>Pour en g\u00e9n\u00e9rer, il existe de nombreuses solutions sur internet\u2026<\/p>\n<p>Afin d\u2019optimiser ses applications Android, il vaudrait mieux avoir des images statiques afin de ne pas consommer de CPU sur les p\u00e9riph\u00e9riques o\u00f9 l\u2019application est d\u00e9ploy\u00e9e\u2026 Cependant, on peut parfois avoir besoin d\u2019en g\u00e9n\u00e9rer de fa\u00e7on dynamique\u2026<\/p>\n<p>Je vous propose dans cet article, une solution assez simple qui fonctionne pour ajouter cette fonctionnalit\u00e9 \u00e0 vos applications&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[132],"tags":[158,27,108],"class_list":["post-1340","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-coloration-syntaxique","tag-script"],"_links":{"self":[{"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/posts\/1340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/comments?post=1340"}],"version-history":[{"count":2,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/posts\/1340\/revisions"}],"predecessor-version":[{"id":6387,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/posts\/1340\/revisions\/6387"}],"wp:attachment":[{"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/media?parent=1340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/categories?post=1340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.wittwer.fr\/whiler\/wp-json\/wp\/v2\/tags?post=1340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}