具有 Rockchip-cs968a 摄像头问题的 Android 智能电视盒

Posted

技术标签:

【中文标题】具有 Rockchip-cs968a 摄像头问题的 Android 智能电视盒【英文标题】:Android Smart TV box having Rockchip-cs968a Camera Issue 【发布时间】:2014-12-30 03:22:20 【问题描述】:

我正在使用具有 Rockchip-cs968a 的 android 智能电视盒。 我无法捕捉图像。 它在运行时引发此设备错误

这是我正在使用的代码:

package com.android.camerarecorder;

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.hardware.Camera.CameraInfo;
    import android.hardware.Camera.PictureCallback;
    import android.os.Environment;
    import android.util.Log;
    import android.view.SurfaceView;

    public class CameraController 

        private Context context;

        private boolean hasCamera;

        private Camera camera;
        private int cameraId;

        public CameraController(Context c)
            context = c.getApplicationContext();

            if(context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
                cameraId = getFrontCameraId();

                if(cameraId != -1)
                    hasCamera = true;
                else
                    hasCamera = false;
                
            else
                hasCamera = false;
            
        

        public boolean hasCamera()
            return hasCamera;
        

        public void getCameraInstance()
            camera = null;

            if(hasCamera)
                try
                  //  camera = Camera.open(cameraId);
                     camera = Camera.open(cameraId);
                    prepareCamera();
                
                catch(Exception e)
                    hasCamera = false;
                
            
        

        public void takePicture()
            if(hasCamera)
                camera.takePicture(null,null,mPicture);
            
        

        public void releaseCamera()
            if(camera != null)
                camera.stopPreview();
                camera.release();
                camera = null;
            
        

        private int getFrontCameraId()
            int camId = -1;
            int numberOfCameras = Camera.getNumberOfCameras();
            CameraInfo ci = new CameraInfo();

            for(int i = 0;i < numberOfCameras;i++)
                Camera.getCameraInfo(i,ci);
                if(ci.facing == CameraInfo.CAMERA_FACING_FRONT)
                    camId = i;
                
            

            return camId;
        

        private void prepareCamera()
            SurfaceView view = new SurfaceView(context);

            try
                camera.setPreviewDisplay(view.getHolder());
            catch(IOException e)
                throw new RuntimeException(e);
            

            camera.startPreview();

            Camera.Parameters params = camera.getParameters();
            params.setJpegQuality(100);

            camera.setParameters(params);  
        

        private PictureCallback mPicture = new PictureCallback()
            @Override
            public void onPictureTaken(byte[] data, Camera camera)
                File pictureFile = getOutputMediaFile();

                if(pictureFile == null)
                    Log.d("TEST", "Error creating media file, check storage permissions");
                    return;
                

                try
                    Log.d("TEST","File created");
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                catch(FileNotFoundException e)
                    Log.d("TEST","File not found: "+e.getMessage());
                 catch (IOException e)
                    Log.d("TEST","Error accessing file: "+e.getMessage());
                
            
        ;

        private File getOutputMediaFile()
            // To be safe, you should check that the SDCard is mounted
            // using Environment.getExternalStorageState() before doing this.

            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyCameraApp");

            // This location works best if you want the created images to be shared
            // between applications and persist after your app has been uninstalled.

            // Create the storage directory if it does not exist
            if(!mediaStorageDir.exists())
                if(!mediaStorageDir.mkdirs())
                    return null;
                
            

            // Create a media file name
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            File mediaFile;
            mediaFile = new File(mediaStorageDir.getPath()+File.separator+"IMG_"+timeStamp+".jpg");

            return mediaFile;
        
    

【问题讨论】:

i am not able to capture image via code 。您并不是一开始就试图捕捉一张照片。您没有展示如何使用 CameraController 类。 尝试 cc.getCameraInstance(); cc.takePicture(); catch (Exception e) // TODO 自动生成的 catch 块 e.printStackTrace(); 在内置相机应用中使用 photospherehelper, tinyplanet 抛出哪个错误?在哪个语句/代码上?请发布 LogCat。您没有在 getCameraInstance() 之后检查 hasCamera。 调用拍照catch子句后显示运行时拍照失败的异常 【参考方案1】:

我发现我正在使用的设备无法使用

com.android.hardware.camera

我浏览了开发者文档,发现 21 以上版本的这个 api 已被弃用

现在他们正在使用

com.android.hardware.camera2包含一些不同类型的相机调用

【讨论】:

以上是关于具有 Rockchip-cs968a 摄像头问题的 Android 智能电视盒的主要内容,如果未能解决你的问题,请参考以下文章

贪心算法:我要监控二叉树!

力扣算法JS LC [714. 买卖股票的最佳时机含手续费] LC [968. 监控二叉树]

在具有多个摄像头的移动设备上选择默认的备用后置摄像头

如何调整这个视图?获取时间需要 9.968,但我想要 0.5。那么如何提供更好的性能

LC 968. Binary Tree Cameras

具有网络摄像头连接的 Java ANPR 项目