Create Matrix for setting Camera - Android android.hardware

Android examples for android.hardware:Camera

Description

Create Matrix for setting Camera

Demo Code

/*/*from   w  w  w .ja va 2  s  . c om*/
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.RectF;

public class Main {

  public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, int viewWidth,
      int viewHeight) {
    // Need mirror for front camera.
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);
    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // UI coordinates range from (0, 0) to (width, height).
    matrix.postScale(viewWidth / 2000f, viewHeight / 2000f);
    matrix.postTranslate(viewWidth / 2f, viewHeight / 2f);
  }

  public static void prepareMatrix(Matrix matrix, boolean mirror, int displayOrientation, Rect previewRect) {
    // Need mirror for front camera.
    matrix.setScale(mirror ? -1 : 1, 1);
    // This is the value for android.hardware.Camera.setDisplayOrientation.
    matrix.postRotate(displayOrientation);

    // Camera driver coordinates range from (-1000, -1000) to (1000, 1000).
    // We need to map camera driver coordinates to preview rect coordinates
    Matrix mapping = new Matrix();
    mapping.setRectToRect(new RectF(-1000, -1000, 1000, 1000), rectToRectF(previewRect), Matrix.ScaleToFit.FILL);
    matrix.setConcat(mapping, matrix);
  }

  public static RectF rectToRectF(Rect r) {
    return new RectF(r.left, r.top, r.right, r.bottom);
  }

}

Related Tutorials