相机的SurfaceView在Android Lollipop OS中不起作用

莫汉·拉杰S

今天,我在android中遇到了一个问题surfaceview for camera customization我尝试了以下代码。

当我捕获图像时发生问题,它停止了相机预览并且没有返回活动。

以下代码将在程序中实现。我从关于stackoverflow的现有参考中获取了这段代码

  1. 配套班。

    public class AndroidCameraSurfaceview extends Activity implements
        SurfaceHolder.Callback {
    TextView testView;
    
    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    boolean preview;
    
    PictureCallback rawCallback;
    ShutterCallback shutterCallback;
    PictureCallback jpegCallback;
    int displayheight, displaywidth;
    Camera.PreviewCallback previewCallback;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.camerasurfaceview);
    
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
    
        surfaceHolder.addCallback(this);
    
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    
        jpegCallback = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                Bundle b = new Bundle();
                b.putByteArray("Image", data);
                Intent intent = new Intent();
                intent.putExtras(b);
                setResult(RESULT_OK, intent);
                finish();
                // refreshCamera();
            }
        };
    }
    
    
    
    public void captureImage(View v) throws IOException {
                // take the picture
                camera.takePicture(null, null, jpegCallback);
            }
    
        public void refreshCamera() {
            if (surfaceHolder.getSurface() == null) {
                // preview surface does not exist
                return;
            }
    
            try {
                camera.stopPreview();
            } catch (Exception e) {
            }
    
            try {
                camera.setDisplayOrientation(90);
                camera.setPreviewDisplay(surfaceHolder);
                camera.startPreview();
            } catch (Exception e) {
    
            }
        }
    
        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            if (preview) {
                camera.stopPreview();
            }
    try{
            Camera.Parameters parameters = camera.getParameters();
            List<Size> sizes = parameters.getSupportedPreviewSizes();
            Size optimalSize = getOptimalPreviewSize(sizes, width, height);
            parameters.setPreviewSize(optimalSize.width, optimalSize.height);
            camera.setParameters(parameters);
            try {
                camera.setDisplayOrientation(90);
                camera.setPreviewDisplay(holder);
                camera.startPreview();
                preview = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
    }catch(Exception e){
        System.out.println("Surface Exception---=>"+e);
    }
        }
    
        public void surfaceCreated(SurfaceHolder holder) {
            camera = Camera.open();
            if (camera != null) {
                Camera.Parameters params = camera.getParameters();
                camera.setDisplayOrientation(90);
                camera.setParameters(params);
    
            }
        }
    
        public void surfaceDestroyed(SurfaceHolder holder) {
            // stop preview and release camera
            camera.stopPreview();
            camera.release();
            camera = null;
        }
    
        private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) {
            final double ASPECT_TOLERANCE = 1;
            double targetRatio = (double) w / h;
            if (sizes == null)
                return null;
            Size optimalSize = null;
            double minDiff = Double.MAX_VALUE;
            int targetHeight = h;
            for (Size size : sizes) {
                double ratio = (double) size.width / size.height;
                if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
                    continue;
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
    
            if (optimalSize == null) {
                minDiff = Double.MAX_VALUE;
                for (Size size : sizes) {
                    if (Math.abs(size.height - targetHeight) < minDiff) {
                        optimalSize = size;
                        minDiff = Math.abs(size.height - targetHeight);
                    }
                }
            }
            return optimalSize;
        }
    
        }
    

    2.参加活动

    公共无效captureImage(){

        Intent intentDriver = new Intent(AddNewDevice_Activity.this,
                AndroidCameraSurfaceview.class);
        startActivityForResult(intentDriver, 0);
        //
        // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //
        // Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        //
        // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        //
        // // start the image capture Intent
        // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    
        // Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //
        // fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        //
        // intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        //
        // // start the image capture Intent
        // startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        super.onActivityResult(requestCode, resultCode, data);
    
        if (requestCode == 0) {
    
            System.out.println("Result Code: " + resultCode);
            if (resultCode == RESULT_OK && data != null) {
    
                Bundle bundle = data.getExtras();
                byte[] test = bundle.getByteArray("Image");
                Bitmap bpCamera = BitmapFactory.decodeByteArray(test, 0,
                        test.length);
                Matrix matrix = new Matrix();
                matrix.postRotate(90);
                bpCamera = Bitmap
                        .createBitmap(bpCamera, 0, 0, bpCamera.getWidth(),
                                bpCamera.getHeight(), matrix, true);
    
                imageView_camera.setImageBitmap(bpCamera);
                selectedImageStr = encodeTobase64(bpCamera);
            }
    
        } else {
            finish();
        }
    
    }
    
阿伦卡提克·纳拉萨米

Google已从21版API修订了相机API,此后,我们必须采用新的camera2软件包,并且在相机功能出现时必须遵守。这是由Google发布的示例代码的链接,该示例代码使用表面视图实现,并且在Android 5.0中完美运行。我相信这解决了一些神秘问题。

https://github.com/googlesamples/android-Camera2Basic

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Android Switch小部件textOn和textOff在Lollipop中不起作用

Android Lollipop上的海拔高度不起作用

Android 5.0(Lollipop)上的海拔高度不起作用

Android Lollipop成为设备管理员不起作用

操作栏中的“主页”按钮在Lollipop中不起作用

自Android 5.0 Lollipop以来,adb shell dumpsys iphonesubinfo不起作用

在Android 5.0 Lollipop中处理媒体按钮

Cordova会话Cookie在Android Lollipop上不起作用

无法解密Android Lollipop中的加密文件

具有相机2 API的Android Lollipop上的ZSL功能

Android Lollipop中的Monospace发生了什么?

动画列表在Android 5.0(Lollipop)中不起作用

对话框FLAG_DIM_BEHIND在Android 5.x(Lollipop)上不起作用

在android 6.0.1中,检索相机照片不起作用

AlarmManager在Lollipop中的Samsung设备上不起作用

自定义进度可绘制在Android Lollipop(API 21)设备上不起作用

无法在Android Lollipop中按时触发AlarmManager

Android Lollipop及更高版本中的TextInputLayout崩溃

相机允许不起作用android webview

代码在与Android / Oxygen OS相关的手机中不起作用,但在银河手机中起作用

ANDROID相机不起作用

Android 5 Lollipop,android:scaleX,android:translationX,Y在Android Studio布局预览中不起作用

列出Android Lollipop中的蓝牙设备

Android Studio中的Android Lollipop系统图标

自定义视图在Android Lollipop中不起作用

Android Lollipop中的信号33是什么?

Theme.AppCompat.DayNight在Android Lollipop上不起作用

动画在SurfaceView中不起作用

HorizontalScrollView 中的表格布局 setStretchAllColumns(true) 在 Lollipop 中不起作用