Android Open Source - ScalAR Camera Parameters






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

/**
  Copyright (C) 2009,2010  Tobias Domhan
//from w  w w.  j a v a2 s .c om
    This file is part of AndOpenGLCam.

    AndObjViewer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AndObjViewer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with AndObjViewer.  If not, see <http://www.gnu.org/licenses/>.
 
 */
package edu.dhbw.andar;

import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import edu.dhbw.andar.exceptions.AndARRuntimeException;
import edu.dhbw.andar.util.GraphicsUtil;

/**
 * Only the newer versions of the SDK support querying the available preview
 * sizes. This class and it descendants encapsulate this functionality, so that
 * no verify exception is thrown on older(1.6) mobile phones.
 * 
 * @author Tobi
 * 
 */
public class CameraParameters {
  private static Method getSupportedPreviewFormats = null;
  private static Method getSupportedPreviewSizes = null;
  static {
    initCompatibility();
  };

  private static void initCompatibility() {
    //separate exception handling is needed, in case on method is supported, the other not
    try {
      getSupportedPreviewSizes = Parameters.class.getMethod(
          "getSupportedPreviewSizes", (Class[]) null);
      /* success, this is a newer device */
    } catch (NoSuchMethodException nsme) {
      /* failure, must be older device */
    }
    try {
      getSupportedPreviewFormats = Parameters.class.getMethod(
          "getSupportedPreviewFormats", (Class[]) null);
      /* success, this is a newer device */
    } catch (NoSuchMethodException nsme) {
      /* failure, must be older device */
    }
  }

  @SuppressWarnings("unchecked")
  public static void setCameraParameters(Camera camera, int width, int height) {
    // reduce preview frame size for performance reasons
    if (getSupportedPreviewSizes != null) {
      Parameters params = camera.getParameters();
      // since SDK 5 web can query the available preview sizes
      // let's choose the smallest available preview size
      List<Size> sizes;
      try {
        Object supportedFormats = getSupportedPreviewSizes.invoke(
            params, (Object[]) null);
        if (supportedFormats instanceof List<?>) {
          sizes = (List<Camera.Size>) supportedFormats;// params.getSupportedPreviewSizes();
          Size optimalSize = GraphicsUtil.getOptimalPreviewSize(
              sizes, width, height);
          Size currentSize = params.getPreviewSize();
          if (!(optimalSize.height == currentSize.height && optimalSize.width == currentSize.width)) {
            // the optimal size was not set, yet. so let's do so now
            Log.d("AndAR", "'query preview sizes' available, setting size to: "+width+" x "+height);
            params.setPreviewSize(optimalSize.width,
                optimalSize.height);
            try {
              camera.setParameters(params);
            } catch (RuntimeException ex) {
              ex.printStackTrace();
            }
          }
        }
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }
    } else {
      // we don't have any information about available previewsizes...
      Parameters params = camera.getParameters();
      Size currentSize = params.getPreviewSize();
      if (!(160 == currentSize.height && 240 == currentSize.width)) {
        // try to set the preview size to this fixed value
        params.setPreviewSize(240, 160);
        try {
          camera.setParameters(params);
        } catch (RuntimeException ex) {
          ex.printStackTrace();
        }
      }
    }

    // now set the pixel format of the preview frames:
    if (getSupportedPreviewFormats != null) {
      Parameters params = camera.getParameters();
      // we may query the available pixelformats in newer SDk versions
      List<Integer> supportedFormats;
      try {
        supportedFormats = (List<Integer>) getSupportedPreviewFormats
            .invoke(params, (Object[]) null);
        if(supportedFormats != null) {
          int format = CameraPreviewHandler
              .getBestSupportedFormat(supportedFormats);
          if (format != -1) {
            params.setPreviewFormat(format);
            try {
              camera.setParameters(params);
            } catch (RuntimeException ex) {
              ex.printStackTrace();
            }
          } else {
            throw new AndARRuntimeException("Unkown pixel format");
          }  
        }
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalAccessException e) {
        e.printStackTrace();
      } catch (InvocationTargetException e) {
        e.printStackTrace();
      }

    } else {
      Parameters params = camera.getParameters();
      if(params.getPreviewFormat() != PixelFormat.YCbCr_420_SP) {
        // try to set the preview format, if it was not YCbCr_420 already
        params.setPreviewFormat(PixelFormat.YCbCr_420_SP);
        try {
          camera.setParameters(params);
        } catch (RuntimeException ex) {
          ex.printStackTrace();
        }
      }
    }
  }

}




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