• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.cts.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 
25 import java.io.ByteArrayInputStream;
26 import java.io.ByteArrayOutputStream;
27 import java.lang.reflect.Method;
28 import java.util.Random;
29 
30 public class BitmapUtils {
BitmapUtils()31     private BitmapUtils() {}
32 
33     // Compares two bitmaps by pixels.
compareBitmaps(Bitmap bmp1, Bitmap bmp2)34     public static boolean compareBitmaps(Bitmap bmp1, Bitmap bmp2) {
35         if (bmp1 == bmp2) {
36             return true;
37         }
38 
39         if (bmp1 == null || bmp2 == null) {
40             return false;
41         }
42 
43         if ((bmp1.getWidth() != bmp2.getWidth()) || (bmp1.getHeight() != bmp2.getHeight())) {
44             return false;
45         }
46 
47         for (int i = 0; i < bmp1.getWidth(); i++) {
48             for (int j = 0; j < bmp1.getHeight(); j++) {
49                 if (bmp1.getPixel(i, j) != bmp2.getPixel(i, j)) {
50                     return false;
51                 }
52             }
53         }
54         return true;
55     }
56 
generateRandomBitmap(int width, int height)57     public static Bitmap generateRandomBitmap(int width, int height) {
58         final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
59         final Random generator = new Random();
60         for (int x = 0; x < width; x++) {
61             for (int y = 0; y < height; y++) {
62                 bmp.setPixel(x, y, generator.nextInt(Integer.MAX_VALUE));
63             }
64         }
65         return bmp;
66     }
67 
generateWhiteBitmap(int width, int height)68     public static Bitmap generateWhiteBitmap(int width, int height) {
69         final Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
70         for (int x = 0; x < width; x++) {
71             for (int y = 0; y < height; y++) {
72                 bmp.setPixel(x, y, Color.WHITE);
73             }
74         }
75         return bmp;
76     }
77 
getWallpaperBitmap(Context context)78     public static Bitmap getWallpaperBitmap(Context context) throws Exception {
79         WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
80         Class<?> noparams[] = {};
81         Class<?> wmClass = wallpaperManager.getClass();
82         Method methodGetBitmap = wmClass.getDeclaredMethod("getBitmap", noparams);
83         return (Bitmap) methodGetBitmap.invoke(wallpaperManager, null);
84     }
85 
bitmapToInputStream(Bitmap bmp)86     public static ByteArrayInputStream bitmapToInputStream(Bitmap bmp) {
87         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
88         bmp.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
89         byte[] bitmapData = bos.toByteArray();
90         return new ByteArrayInputStream(bitmapData);
91     }
92 }
93