Home | History | Annotate | Download | only in devcamera
      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 package com.android.devcamera;
     17 
     18 import android.graphics.Bitmap;
     19 import android.graphics.BitmapFactory;
     20 import android.graphics.Matrix;
     21 import android.media.Image;
     22 
     23 import java.nio.ByteBuffer;
     24 
     25 /**
     26  * Some Bitmap utility functions.
     27  */
     28 public class BitmapUtility {
     29 
     30     public static Bitmap bitmapFromJpeg(byte[] data) {
     31         // 32K buffer.
     32         byte[] decodeBuffer = new byte[32 * 1024]; // 32K buffer.
     33 
     34         BitmapFactory.Options opts = new BitmapFactory.Options();
     35         opts.inSampleSize = 16; // 3264 / 16 = 204.
     36         opts.inTempStorage = decodeBuffer;
     37         Bitmap b = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
     38 
     39         return rotatedBitmap(b);
     40     }
     41 
     42     public static Bitmap bitmapFromYuvImage(Image img) {
     43         int w = img.getWidth();
     44         int h = img.getHeight();
     45         ByteBuffer buf0 = img.getPlanes()[0].getBuffer();
     46         int len = buf0.capacity();
     47         int[] colors = new int[len];
     48         int alpha = 255 << 24;
     49         int green;
     50         for (int i = 0; i < len; i++) {
     51             green = ((int) buf0.get(i)) & 255;
     52             colors[i] = green << 16 | green << 8 | green | alpha;
     53         }
     54         Bitmap b = Bitmap.createBitmap(colors, w, h, Bitmap.Config.ARGB_8888);
     55 
     56         return rotatedBitmap(b);
     57     }
     58 
     59     /**
     60      * Returns parameter bitmap rotated 90 degrees
     61      */
     62     private static Bitmap rotatedBitmap(Bitmap b) {
     63         Matrix mat = new Matrix();
     64         mat.postRotate(90);
     65         Bitmap b2 = Bitmap.createBitmap(b, 0, 0,b.getWidth(),b.getHeight(), mat, true);
     66         return b2;
     67     }
     68 
     69 }
     70