Juin
21
|
On voit de plus en plus des images comme celle-ci…
… les codes QR !
Pour en générer, il existe de nombreuses solutions sur internet…
Afin d’optimiser ses applications AndroidW, il vaudrait mieux avoir des images statiques afin de ne pas consommer de CPU sur les périphériques où l’application est déployée… Cependant, on peut parfois avoir besoin d’en générer de façon dynamique…
Je vous propose dans cet article, une solution assez simple qui fonctionne pour ajouter cette fonctionnalité à vos applications.
Pour l’implémenter, j’ai téléchargé la solution Java de Kazuhiko Arase, et je l’ai légèrement modifiée pour qu’elle fonctionne avec le SDK d’Android :
Entre autre, la façon de modifier les pixels d’une image en mémoire…
Avant :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public BufferedImage createImage(int cellSize, int margin) throws IOException { int imageSize = getModuleCount() * cellSize + margin * 2; BufferedImage image = new BufferedImage(imageSize, imageSize, BufferedImage.TYPE_INT_RGB); for (int y = 0; y < imageSize; y++) { for (int x = 0; x < imageSize; x++) { if (margin <= x && x < imageSize - margin && margin <= y && y < imageSize - margin) { int col = (x - margin) / cellSize; int row = (y - margin) / cellSize; if (isDark(row, col) ) { image.setRGB(x, y, 0x000000); } else { image.setRGB(x, y, 0xffffff); } } else { image.setRGB(x, y, 0xffffff); } } } return image; } |
Après :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public Bitmap createImage(int cellSize, int margin, int cForeground, int cBackground, int cMargin) throws IOException { int imageSize = getModuleCount() * cellSize + margin * 2; Bitmap image = Bitmap.createBitmap(imageSize, imageSize, Bitmap.Config.ARGB_8888); for (int y = 0; y < imageSize; y++) { for (int x = 0; x < imageSize; x++) { if (margin <= x && x < imageSize - margin && margin <= y && y < imageSize - margin) { int col = (x - margin) / cellSize; int row = (y - margin) / cellSize; if (isDark(row, col) ) { image.setPixel(x, y, cForeground); } else { image.setPixel(x, y, cBackground); } } else { image.setPixel(x, y, cMargin); } } } return image; } |
Vous pouvez télécharger le code source depuis ce lien : Code source
Sur la page suivante, je vais vous montrer comment l’utiliser pour faire une implémentation des plus simples…
Pages : 1 2
Un grand, mais très grand merci pour ce code
@ Alexis :
génial merci mon ami
@ geo :
Bonjour, j’arrive pas a charger le programme
@ nobl : Bonjour,
Je viens de tester… le téléchargement fonctionne sans problème…
modifié/traduit par Whiler
Politique de confidentialité