Android Open Source - ScalAR Graphics Util






From Project

Back to project page ScalAR.

License

The source code is released under:

GNU General Public License

If you think the Android project ScalAR listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.dhbw.andar.util;
/*ww w  .j ava  2 s.c om*/
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.Iterator;
import java.util.List;

import android.hardware.Camera.Size;

public class GraphicsUtil {
  //private final static double epsilon = 0.001;
  //this epsilon being so large is intended, as often there will not be an adequate resolution with
  //the correct aspect ratio available
  //so we trade the correct aspect ratio for faster rendering
  private final static double epsilon = 0.17;
  
  /**
   * Make a direct NIO FloatBuffer from an array of floats
   * @param arr The array
   * @return The newly created FloatBuffer
   */
  public static FloatBuffer makeFloatBuffer(float[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4);
    bb.order(ByteOrder.nativeOrder());
    FloatBuffer fb = bb.asFloatBuffer();
    fb.put(arr);
    fb.position(0);
    return fb;
  }
  /**
   * Make a direct NIO ByteBuffer from an array of floats
   * @param arr The array
   * @return The newly created FloatBuffer
   */
  public static ByteBuffer makeByteBuffer(byte[] arr) {
    ByteBuffer bb = ByteBuffer.allocateDirect(arr.length);
    bb.order(ByteOrder.nativeOrder());
    bb.put(arr);
    bb.position(0);
    return bb;
  }
  public static ByteBuffer makeByteBuffer(int size) {
    ByteBuffer bb = ByteBuffer.allocateDirect(size);
    bb.position(0);
    return bb;
  }
  
  /**
   * Get the optimal preview size for the given screen size.
   * @param sizes
   * @param screenWidth
   * @param screenHeight
   * @return
   */
  public static Size getOptimalPreviewSize(List<Size> sizes, int screenWidth, int screenHeight) {
    double aspectRatio = ((double)screenWidth)/screenHeight;
    Size optimalSize = null;
    for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
      Size currSize =  iterator.next();
      double curAspectRatio = ((double)currSize.width)/currSize.height;
      //do the aspect ratios equal?
      if ( Math.abs( aspectRatio - curAspectRatio ) < epsilon ) {
        //they do
        if(optimalSize!=null) {
          //is the current size smaller than the one before
          if(optimalSize.height>currSize.height && optimalSize.width>currSize.width) {
            optimalSize = currSize;
          }
        } else {
          optimalSize = currSize;
        }
      }
    }
    if(optimalSize == null) {
      //did not find a size with the correct aspect ratio.. let's choose the smallest instead
      for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
        Size currSize =  iterator.next();
        if(optimalSize!=null) {
          //is the current size smaller than the one before
          if(optimalSize.height>currSize.height && optimalSize.width>currSize.width) {
            optimalSize = currSize;
          } else {
            optimalSize = currSize;
          }
        }else {
          optimalSize = currSize;
        }
        
      }
    }
    return optimalSize;
  }
  
  public static boolean containsSize(List<Size> sizes, Size size) {
    for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
      Size currSize =  iterator.next();
      if(currSize.width == size.width && currSize.height == size.height) {
        return true;
      }      
    }
    return false;
  }
  
  public static Size getSmallestSize(List<Size> sizes) {
    Size optimalSize = null;
    for (Iterator<Size> iterator = sizes.iterator(); iterator.hasNext();) {
      Size currSize =  iterator.next();    
      if(optimalSize == null) {
        optimalSize = currSize;
      } else if(optimalSize.height>currSize.height && optimalSize.width>currSize.width) {
        optimalSize = currSize;
      }
    }
    return optimalSize;
  }
  
}




Java Source Code List

com.skylion.speech.GoogleTranslate.java
com.skylion.speech.Narrator.java
com.skylion.speech.Synthesiser.java
edu.dhbw.andar.ARObject.java
edu.dhbw.andar.ARToolkit.java
edu.dhbw.andar.AndARActivity.java
edu.dhbw.andar.AndARRenderer.java
edu.dhbw.andar.CameraHolder.java
edu.dhbw.andar.CameraParameters.java
edu.dhbw.andar.CameraPreviewHandler.java
edu.dhbw.andar.CameraStatus.java
edu.dhbw.andar.Config.java
edu.dhbw.andar.GenericFunctions.java
edu.dhbw.andar.exceptions.AndARException.java
edu.dhbw.andar.exceptions.AndARRuntimeException.java
edu.dhbw.andar.interfaces.MarkerVisibilityListener.java
edu.dhbw.andar.interfaces.OpenGLRenderer.java
edu.dhbw.andar.interfaces.PreviewFrameSink.java
edu.dhbw.andar.pub.CustomActivity.java
edu.dhbw.andar.pub.CustomObject.java
edu.dhbw.andar.pub.CustomRenderer.java
edu.dhbw.andar.pub.SimpleBox.java
edu.dhbw.andar.util.GraphicsUtil.java
edu.dhbw.andar.util.IO.java
edu.dhbw.andobjviewer.AugmentedModelViewerActivity.java
edu.dhbw.andobjviewer.CheckFileManagerActivity.java
edu.dhbw.andobjviewer.Config.java
edu.dhbw.andobjviewer.InstructionsActivity.java
edu.dhbw.andobjviewer.ModelChooser.java
edu.dhbw.andobjviewer.graphics.LightingRenderer.java
edu.dhbw.andobjviewer.graphics.Model3D.java
edu.dhbw.andobjviewer.graphics.Renderer.java
edu.dhbw.andobjviewer.models.Group.java
edu.dhbw.andobjviewer.models.Material.java
edu.dhbw.andobjviewer.models.Model.java
edu.dhbw.andobjviewer.models.Vector3D.java
edu.dhbw.andobjviewer.parser.MtlParser.java
edu.dhbw.andobjviewer.parser.ObjParser.java
edu.dhbw.andobjviewer.parser.ParseException.java
edu.dhbw.andobjviewer.parser.SimpleTokenizer.java
edu.dhbw.andobjviewer.parser.Util.java
edu.dhbw.andobjviewer.util.ArrayIterator.java
edu.dhbw.andobjviewer.util.AssetsFileUtil.java
edu.dhbw.andobjviewer.util.BaseFileUtil.java
edu.dhbw.andobjviewer.util.FixedPointUtils.java
edu.dhbw.andobjviewer.util.MatrixUtils.java
edu.dhbw.andobjviewer.util.MemUtil.java
edu.dhbw.andobjviewer.util.SDCardFileUtil.java