1 /* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License 15 */ 16 17 package com.android.compatibility.common.util; 18 19 import android.app.WallpaperManager; 20 import android.content.Context; 21 import android.graphics.Bitmap; 22 import android.graphics.Bitmap.CompressFormat; 23 import android.graphics.Color; 24 import android.util.Log; 25 26 import java.io.ByteArrayInputStream; 27 import java.io.ByteArrayOutputStream; 28 import java.io.File; 29 import java.io.FileNotFoundException; 30 import java.io.FileOutputStream; 31 import java.lang.reflect.Method; 32 import java.util.Random; 33 34 public class BitmapUtils { 35 private static final String TAG = "BitmapUtils"; 36 BitmapUtils()37 private BitmapUtils() {} 38 39 // Compares two bitmaps by pixels. compareBitmaps(Bitmap bmp1, Bitmap bmp2)40 public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2) { 41 if (bmp1 == bmp2) { 42 return true; 43 } 44 45 if (bmp1 == null || bmp2 == null) { 46 return false; 47 } 48 49 if ((bmp1.getWidth() != bmp2.getWidth()) || (bmp1.getHeight() != bmp2.getHeight())) { 50 return false; 51 } 52 53 for (int i = 0; i < bmp1.getWidth(); i++) { 54 for (int j = 0; j < bmp1.getHeight(); j++) { 55 if (bmp1.getPixel(i, j) != bmp2.getPixel(i, j)) { 56 return false; 57 } 58 } 59 } 60 return true; 61 } 62 generateRandomBitmap(int width, int height)63 public static Bitmap generateRandomBitmap(int width, int height) { 64 final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 65 final Random generator = new Random(); 66 for (int x = 0; x < width; x++) { 67 for (int y = 0; y < height; y++) { 68 bmp.setPixel(x, y, generator.nextInt(Integer.MAX_VALUE)); 69 } 70 } 71 return bmp; 72 } 73 generateWhiteBitmap(int width, int height)74 public static Bitmap generateWhiteBitmap(int width, int height) { 75 final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 76 bmp.eraseColor(Color.WHITE); 77 return bmp; 78 } 79 getWallpaperBitmap(Context context)80 public static Bitmap getWallpaperBitmap(Context context) throws Exception { 81 WallpaperManager wallpaperManager = WallpaperManager.getInstance(context); 82 Class<?> noparams[] = {}; 83 Class<?> wmClass = wallpaperManager.getClass(); 84 Method methodGetBitmap = wmClass.getDeclaredMethod("getBitmap", noparams); 85 return (Bitmap) methodGetBitmap.invoke(wallpaperManager, null); 86 } 87 bitmapToInputStream(Bitmap bmp)88 public static ByteArrayInputStream bitmapToInputStream(Bitmap bmp) { 89 final ByteArrayOutputStream bos = new ByteArrayOutputStream(); 90 bmp.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos); 91 byte[] bitmapData = bos.toByteArray(); 92 return new ByteArrayInputStream(bitmapData); 93 } 94 logIfBitmapSolidColor(String fileName, Bitmap bitmap)95 private static void logIfBitmapSolidColor(String fileName, Bitmap bitmap) { 96 int firstColor = bitmap.getPixel(0, 0); 97 for (int x = 0; x < bitmap.getWidth(); x++) { 98 for (int y = 0; y < bitmap.getHeight(); y++) { 99 if (bitmap.getPixel(x, y) != firstColor) { 100 return; 101 } 102 } 103 } 104 105 Log.w(TAG, String.format("%s entire bitmap color is %x", fileName, firstColor)); 106 } 107 saveBitmap(Bitmap bitmap, String directoryName, String fileName)108 public static void saveBitmap(Bitmap bitmap, String directoryName, String fileName) { 109 new File(directoryName).mkdirs(); // create dirs if needed 110 111 Log.d(TAG, "Saving file: " + fileName + " in directory: " + directoryName); 112 113 if (bitmap == null) { 114 Log.d(TAG, "File not saved, bitmap was null"); 115 return; 116 } 117 118 logIfBitmapSolidColor(fileName, bitmap); 119 120 File file = new File(directoryName, fileName); 121 try (FileOutputStream fileStream = new FileOutputStream(file)) { 122 bitmap.compress(Bitmap.CompressFormat.PNG, 0 /* ignored for PNG */, fileStream); 123 fileStream.flush(); 124 } catch (Exception e) { 125 e.printStackTrace(); 126 } 127 } 128 } 129