1/*
2 * Copyright (C) 2016 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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.graphics.Bitmap;
21import android.hardware.Camera;
22import android.os.Bundle;
23import android.os.Environment;
24import android.view.Gravity;
25import android.view.PixelCopy;
26import android.view.Surface;
27import android.view.SurfaceHolder;
28import android.view.SurfaceView;
29import android.view.View;
30import android.widget.Button;
31import android.widget.FrameLayout;
32import android.widget.Toast;
33
34import java.io.FileOutputStream;
35import java.io.IOException;
36
37public class GetBitmapSurfaceViewActivity extends Activity implements SurfaceHolder.Callback {
38    private Camera mCamera;
39    private SurfaceView mSurfaceView;
40
41    @Override
42    protected void onCreate(Bundle savedInstanceState) {
43        super.onCreate(savedInstanceState);
44
45        FrameLayout content = new FrameLayout(this);
46
47        mSurfaceView = new SurfaceView(this);
48        mSurfaceView.getHolder().addCallback(this);
49
50        Button button = new Button(this);
51        button.setText("Copy bitmap to /sdcard/surfaceview.png");
52        button.setOnClickListener((View v) -> {
53            final Bitmap b = Bitmap.createBitmap(
54                    mSurfaceView.getWidth(), mSurfaceView.getHeight(),
55                    Bitmap.Config.ARGB_8888);
56            PixelCopy.request(mSurfaceView, b,
57                    (int result) -> {
58                        if (result != PixelCopy.SUCCESS) {
59                            Toast.makeText(GetBitmapSurfaceViewActivity.this,
60                                    "Failed to copy", Toast.LENGTH_SHORT).show();
61                            return;
62                        }
63                        try {
64                            try (FileOutputStream out = new FileOutputStream(
65                                    Environment.getExternalStorageDirectory() + "/surfaceview.png");) {
66                                b.compress(Bitmap.CompressFormat.PNG, 100, out);
67                            }
68                        } catch (Exception e) {
69                            // Ignore
70                        }
71                    }, mSurfaceView.getHandler());
72        });
73
74        content.addView(mSurfaceView, new FrameLayout.LayoutParams(500, 400, Gravity.CENTER));
75        content.addView(button, new FrameLayout.LayoutParams(
76                FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT,
77                Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM));
78        setContentView(content);
79    }
80
81    @Override
82    public void surfaceCreated(SurfaceHolder holder) {
83        mCamera = Camera.open();
84
85        try {
86            mCamera.setPreviewSurface(holder.getSurface());
87        } catch (IOException t) {
88            android.util.Log.e("TextureView", "Cannot set preview texture target!", t);
89        }
90
91        setCameraDisplayOrientation(this, 0, mCamera);
92        mCamera.startPreview();
93    }
94
95    public static void setCameraDisplayOrientation(Activity activity,
96            int cameraId, android.hardware.Camera camera) {
97        android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
98        android.hardware.Camera.getCameraInfo(cameraId, info);
99        int rotation = activity.getWindowManager().getDefaultDisplay()
100                .getRotation();
101        int degrees = 0;
102        switch (rotation) {
103            case Surface.ROTATION_0:
104                degrees = 0;
105                break;
106            case Surface.ROTATION_90:
107                degrees = 90;
108                break;
109            case Surface.ROTATION_180:
110                degrees = 180;
111                break;
112            case Surface.ROTATION_270:
113                degrees = 270;
114                break;
115        }
116
117        int result;
118        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
119            result = (info.orientation + degrees) % 360;
120            result = (360 - result) % 360; // compensate the mirror
121        } else { // back-facing
122            result = (info.orientation - degrees + 360) % 360;
123        }
124        camera.setDisplayOrientation(result);
125    }
126
127    @Override
128    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
129    }
130
131    @Override
132    public void surfaceDestroyed(SurfaceHolder holder) {
133        mCamera.stopPreview();
134        mCamera.release();
135    }
136}
137