Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2011 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.test.hwui;
     18 
     19 import android.app.Activity;
     20 import android.graphics.Bitmap;
     21 import android.graphics.Matrix;
     22 import android.graphics.SurfaceTexture;
     23 import android.hardware.Camera;
     24 import android.os.Bundle;
     25 import android.os.Environment;
     26 import android.view.Gravity;
     27 import android.view.Surface;
     28 import android.view.TextureView;
     29 import android.view.View;
     30 import android.widget.Button;
     31 import android.widget.FrameLayout;
     32 
     33 import java.io.BufferedOutputStream;
     34 import java.io.File;
     35 import java.io.FileNotFoundException;
     36 import java.io.FileOutputStream;
     37 import java.io.IOException;
     38 
     39 @SuppressWarnings({"UnusedDeclaration"})
     40 public class TextureViewActivity extends Activity implements TextureView.SurfaceTextureListener {
     41     private Camera mCamera;
     42     private TextureView mTextureView;
     43     private FrameLayout mContent;
     44     private Matrix mMatrix = new Matrix();
     45 
     46     @Override
     47     protected void onCreate(Bundle savedInstanceState) {
     48         super.onCreate(savedInstanceState);
     49 
     50         mContent = new FrameLayout(this);
     51 
     52         mTextureView = new TextureView(this);
     53         mTextureView.setSurfaceTextureListener(this);
     54         mTextureView.setOnClickListener(new View.OnClickListener() {
     55             @Override
     56             public void onClick(View v) {
     57                 Bitmap b = mTextureView.getBitmap(800, 800);
     58                 BufferedOutputStream out = null;
     59                 try {
     60                     File dump = new File(Environment.getExternalStorageDirectory(), "out.png");
     61                     out = new BufferedOutputStream(new FileOutputStream(dump));
     62                     b.compress(Bitmap.CompressFormat.PNG, 100, out);
     63                 } catch (FileNotFoundException e) {
     64                     e.printStackTrace();
     65                 } finally {
     66                     if (out != null) try {
     67                         out.close();
     68                     } catch (IOException e) {
     69                         e.printStackTrace();
     70                     }
     71                 }
     72             }
     73         });
     74 
     75         Button button = new Button(this);
     76         button.setText("Remove/Add");
     77         button.setOnClickListener(new View.OnClickListener() {
     78             private boolean mAdded = true;
     79 
     80             @Override
     81             public void onClick(View v) {
     82                 if (mAdded) {
     83                     mContent.removeView(mTextureView);
     84                 } else {
     85                     mContent.addView(mTextureView);
     86                 }
     87                 mAdded = !mAdded;
     88             }
     89         });
     90 
     91         mContent.addView(mTextureView, new FrameLayout.LayoutParams(
     92                 FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
     93                 Gravity.CENTER));
     94         mContent.addView(button, new FrameLayout.LayoutParams(
     95                 FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
     96                 Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
     97         setContentView(mContent);
     98     }
     99 
    100     @Override
    101     public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
    102         mCamera = Camera.open();
    103         mCamera.setDisplayOrientation(getCameraOrientation());
    104 
    105         Camera.Size previewSize = mCamera.getParameters().getPreviewSize();
    106         mTextureView.setLayoutParams(new FrameLayout.LayoutParams(
    107                 previewSize.width, previewSize.height, Gravity.CENTER));
    108 
    109         try {
    110             mCamera.setPreviewTexture(surface);
    111         } catch (IOException t) {
    112             android.util.Log.e("TextureView", "Cannot set preview texture target!", t);
    113         }
    114 
    115         mCamera.startPreview();
    116     }
    117 
    118     private int getCameraOrientation() {
    119         Camera.CameraInfo info = new Camera.CameraInfo();
    120         for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
    121             Camera.getCameraInfo(i, info);
    122             if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) break;
    123         }
    124 
    125         int rotation = getWindowManager().getDefaultDisplay().getRotation();
    126         int degrees = 0;
    127 
    128         switch (rotation) {
    129             case Surface.ROTATION_0:
    130                 degrees = 0;
    131                 break;
    132             case Surface.ROTATION_90:
    133                 degrees = 90;
    134                 break;
    135             case Surface.ROTATION_180:
    136                 degrees = 180;
    137                 break;
    138             case Surface.ROTATION_270:
    139                 degrees = 270;
    140                 break;
    141         }
    142 
    143         return (info.orientation - degrees + 360) % 360;
    144     }
    145 
    146     @Override
    147     public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    148         // Ignored, the Camera does all the work for us
    149     }
    150 
    151     @Override
    152     public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
    153         mCamera.stopPreview();
    154         mCamera.release();
    155         return true;
    156     }
    157 
    158     @Override
    159     public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    160         // Ignored
    161     }
    162 }
    163