Home | History | Annotate | Download | only in helper
      1 /*
      2  * Copyright 2013 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.sample.helper;
     18 
     19 import java.io.File;
     20 import java.io.FileInputStream;
     21 
     22 import javax.microedition.khronos.opengles.GL10;
     23 
     24 import android.content.Context;
     25 import android.content.pm.ApplicationInfo;
     26 import android.graphics.Bitmap;
     27 import android.graphics.BitmapFactory;
     28 import android.graphics.Matrix;
     29 import android.media.AudioManager;
     30 import android.media.AudioTrack;
     31 import android.opengl.GLUtils;
     32 import android.util.Log;
     33 
     34 public class NDKHelper
     35 {
     36     private static Context context;
     37 
     38     public static void setContext(Context c)
     39     {
     40         Log.i("NDKHelper", "setContext:" + c);
     41         context = c;
     42     }
     43 
     44     //
     45     // Load Bitmap
     46     // Java helper is useful decoding PNG, TIFF etc rather than linking libPng
     47     // etc separately
     48     //
     49     private int nextPOT(int i)
     50     {
     51         int pot = 1;
     52         while (pot < i)
     53             pot <<= 1;
     54         return pot;
     55     }
     56 
     57     private Bitmap scaleBitmap(Bitmap bitmapToScale, float newWidth, float newHeight)
     58     {
     59         if (bitmapToScale == null)
     60             return null;
     61         // get the original width and height
     62         int width = bitmapToScale.getWidth();
     63         int height = bitmapToScale.getHeight();
     64         // create a matrix for the manipulation
     65         Matrix matrix = new Matrix();
     66 
     67         // resize the bit map
     68         matrix.postScale(newWidth / width, newHeight / height);
     69 
     70         // recreate the new Bitmap and set it back
     71         return Bitmap.createBitmap(bitmapToScale, 0, 0, bitmapToScale.getWidth(),
     72                 bitmapToScale.getHeight(), matrix, true);
     73     }
     74 
     75     public boolean loadTexture(String path)
     76     {
     77         Bitmap bitmap = null;
     78         try
     79         {
     80             String str = path;
     81             if (!path.startsWith("/"))
     82             {
     83                 str = "/" + path;
     84             }
     85 
     86             File file = new File(context.getExternalFilesDir(null), str);
     87             if (file.canRead())
     88             {
     89                 bitmap = BitmapFactory.decodeStream(new FileInputStream(file));
     90             } else
     91             {
     92                 bitmap = BitmapFactory.decodeStream(context.getResources().getAssets()
     93                         .open(path));
     94             }
     95             // Matrix matrix = new Matrix();
     96             // // resize the bit map
     97             // matrix.postScale(-1F, 1F);
     98             //
     99             // // recreate the new Bitmap and set it back
    100             // bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
    101             // bitmap.getHeight(), matrix, true);
    102 
    103         } catch (Exception e)
    104         {
    105             Log.w("NDKHelper", "Coundn't load a file:" + path);
    106             return false;
    107         }
    108 
    109         if (bitmap != null)
    110         {
    111             GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
    112         }
    113         return true;
    114 
    115     }
    116 
    117     public Bitmap openBitmap(String path, boolean iScalePOT)
    118     {
    119         Bitmap bitmap = null;
    120         try
    121         {
    122             bitmap = BitmapFactory.decodeStream(context.getResources().getAssets()
    123                     .open(path));
    124             if (iScalePOT)
    125             {
    126                 int originalWidth = getBitmapWidth(bitmap);
    127                 int originalHeight = getBitmapHeight(bitmap);
    128                 int width = nextPOT(originalWidth);
    129                 int height = nextPOT(originalHeight);
    130                 if (originalWidth != width || originalHeight != height)
    131                 {
    132                     // Scale it
    133                     bitmap = scaleBitmap(bitmap, width, height);
    134                 }
    135             }
    136 
    137         } catch (Exception e)
    138         {
    139             Log.w("NDKHelper", "Coundn't load a file:" + path);
    140         }
    141 
    142         return bitmap;
    143     }
    144 
    145     public int getBitmapWidth(Bitmap bmp)
    146     {
    147         return bmp.getWidth();
    148     }
    149 
    150     public int getBitmapHeight(Bitmap bmp)
    151     {
    152         return bmp.getHeight();
    153     }
    154 
    155     public void getBitmapPixels(Bitmap bmp, int[] pixels)
    156     {
    157         int w = bmp.getWidth();
    158         int h = bmp.getHeight();
    159         bmp.getPixels(pixels, 0, w, 0, 0, w, h);
    160     }
    161 
    162     public void closeBitmap(Bitmap bmp)
    163     {
    164         bmp.recycle();
    165     }
    166 
    167     public static String getNativeLibraryDirectory(Context appContext)
    168     {
    169         ApplicationInfo ai = context.getApplicationInfo();
    170 
    171         Log.w("NDKHelper", "ai.nativeLibraryDir:" + ai.nativeLibraryDir);
    172 
    173         if ((ai.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0
    174                 || (ai.flags & ApplicationInfo.FLAG_SYSTEM) == 0)
    175         {
    176             return ai.nativeLibraryDir;
    177         }
    178         return "/system/lib/";
    179     }
    180 
    181     public int getNativeAudioBufferSize()
    182     {
    183         int SDK_INT = android.os.Build.VERSION.SDK_INT;
    184         if (SDK_INT >= 17)
    185         {
    186             AudioManager am = (AudioManager) context
    187                     .getSystemService(Context.AUDIO_SERVICE);
    188             String framesPerBuffer = am
    189                     .getProperty(AudioManager.PROPERTY_OUTPUT_FRAMES_PER_BUFFER);
    190             return Integer.parseInt(framesPerBuffer);
    191         } else
    192         {
    193             return 0;
    194         }
    195     }
    196 
    197     public int getNativeAudioSampleRate()
    198     {
    199         return AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    200 
    201     }
    202 
    203 }
    204