Home | History | Annotate | Download | only in captureintent
      1 /*
      2  * Copyright (C) 2015 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.camera.captureintent;
     18 
     19 import android.graphics.Bitmap;
     20 import android.graphics.BitmapFactory;
     21 import android.graphics.Matrix;
     22 
     23 public class PictureDecoder {
     24     /**
     25      * Decodes a jpeg byte array into a Bitmap object.
     26      *
     27      * @param data a byte array of jpeg data
     28      * @param downSampleFactor down-sample factor
     29      * @param pictureOrientation The picture orientation in degrees.
     30      * @param needMirror Whether the bitmap should be flipped horizontally.
     31      * @return decoded and down-sampled bitmap
     32      */
     33     public static Bitmap decode(
     34             byte[] data, int downSampleFactor, int pictureOrientation, boolean needMirror) {
     35         // Downsample the image
     36         final BitmapFactory.Options opts = new BitmapFactory.Options();
     37         opts.inSampleSize = downSampleFactor;
     38         final Bitmap pictureBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opts);
     39         if (pictureOrientation == 0 && !needMirror) {
     40             return pictureBitmap;
     41         }
     42 
     43         Matrix m = new Matrix();
     44         // Rotate if needed.
     45         if (pictureOrientation != 0) {
     46             m.setRotate(pictureOrientation);
     47         }
     48         // Flip horizontally if needed.
     49         if (needMirror) {
     50             m.postScale(-1f, 1f);
     51         }
     52         return Bitmap.createBitmap(
     53                 pictureBitmap, 0, 0, pictureBitmap.getWidth(), pictureBitmap.getHeight(), m, false);
     54     }
     55 }
     56