在相机上设置实时帧捕获图像Android(错误)

时间:2017-12-18 00:32:33

标签: image camera overlay frame implementation

我在下面实现此代码时遇到错误。 我在CameraActivity.java上收到错误(请参阅我的图片附件) 有谁能解决这个问题?

这是CameraActivity.java

public class CameraActivity extends AppCompatActivity implements SurfaceHolder.Callback{

    private Camera camera = null;
    private SurfaceView cameraSurfaceView = null;
    private SurfaceHolder cameraSurfaceHolder = null;
    private boolean previewing = false;

    private Display display = null;


    private static int wid = 0, hgt = 0;

    private LayoutInflater layoutInflater = null;
    private View cameraViewControl = null;
    private ActionBar.LayoutParams layoutParamsControl = null;

    private Button btnCapture = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        display = getWindowManager().getDefaultDisplay();
        wid = display.getWidth();
        hgt = display.getHeight();

        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.cameraoverlay);

        cameraSurfaceView = (SurfaceView)findViewById(R.id.cameraSurfaceView);
        cameraSurfaceHolder = cameraSurfaceView.getHolder();
        cameraSurfaceHolder.addCallback(this);
        cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        layoutInflater = LayoutInflater.from(getBaseContext());
        layoutParamsControl = new ActionBar.LayoutParams(ActionBar.LayoutParams.FILL_PARENT, ActionBar.LayoutParams.FILL_PARENT);

        cameraViewControl = layoutInflater.inflate(R.layout.cambutton,null);
        this.addContentView(cameraViewControl, layoutParamsControl);
        btnCapture = (Button)findViewById(R.id.btnCapture);
        btnCapture.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                // TODO Auto-generated method stub
                camera.takePicture(cameraShutterCallback,
                        cameraPictureCallbackRaw,
                        cameraPictureCallbackJpeg);
            }
        });
    }

    CameraSource.ShutterCallback cameraShutterCallback = new CameraSource.ShutterCallback()
    {
        @Override
        public void onShutter()
        {
            // TODO Auto-generated method stub
        }
    };

    CameraSource.PictureCallback cameraPictureCallbackRaw = new CameraSource.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] bytes) {

        }


        public void onPictureTaken(byte[] data, Camera camera)
        {
            // TODO Auto-generated method stub
        }
    };

    CameraSource.PictureCallback cameraPictureCallbackJpeg = new CameraSource.PictureCallback()
    {
        @Override
        public void onPictureTaken(byte[] bytes) {

        }


        public void onPictureTaken(byte[] data, Camera camera)
        {
            // TODO Auto-generated method stub
            Bitmap cameraBitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

            wid = cameraBitmap.getWidth();
            hgt = cameraBitmap.getHeight();

            Bitmap newImage = Bitmap.createBitmap(wid, hgt, Bitmap.Config.ARGB_8888);

            Canvas canvas = new Canvas(newImage);

            canvas.drawBitmap(cameraBitmap, 0f, 0f, null);

            Drawable drawable = getResources().getDrawable
                    (R.drawable.frame);
            drawable.setBounds(0, 0, wid, hgt);
            drawable.draw(canvas);

            File storagePath = new File(Environment.
                    getExternalStorageDirectory() + "/MyCameraApp/");
            storagePath.mkdirs();

            File myImage = new File(storagePath,
                    Long.toString(System.currentTimeMillis()) + ".jpg");

            try
            {
                FileOutputStream out = new FileOutputStream(myImage);
                newImage.compress(Bitmap.CompressFormat.JPEG, 80, out);


                out.flush();
                out.close();
            }
            catch(FileNotFoundException e)
            {
                Log.d("In Saving File", e + "");
            }
            catch(IOException e)
            {
                Log.d("In Saving File", e + "");
            }

            camera.startPreview();

            drawable = null;

            newImage.recycle();
            newImage = null;

            cameraBitmap.recycle();
            cameraBitmap = null;
        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder,
                               int format, int width, int height)
    {
        // TODO Auto-generated method stub

        if(previewing)
        {
            camera.stopPreview();
            previewing = false;
        }
        if (camera != null){
            try
            {
                camera.setPreviewDisplay(cameraSurfaceHolder);
                camera.startPreview();
                previewing = true;
            }
            catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        try
        {
            camera = Camera.open();
        }
        catch(RuntimeException e)
        {
            Toast.makeText(getApplicationContext(), "Device camera is not working properly, please try after sometime.", Toast.LENGTH_LONG).show();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        // TODO Auto-generated method stub
        camera.stopPreview();
        camera.release();
        camera = null;
        previewing = false;
    }}

this is an error on CameraActivity.java

这是cambutton.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    android:orientation="vertical" >
    <Button
        android:id="@+id/btnCapture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@drawable/cambutton"
        android:gravity="center_vertical" /> </LinearLayout>

这是cameraoverlay.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/cameraSurfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/frame" />
</LinearLayout>

希望任何人都可以修复此代码...... 我用android studio 3.0.1编译

3 个答案:

答案 0 :(得分:0)

尝试添加:

import android.hardware.Camera;

答案 1 :(得分:0)

让我们从基本语句开始:Java不是一种鸭式语言。这意味着如果两个类A和B都定义了方法 foo(),则不能在期望A类对象的上下文中使用类B的对象,即使您只执行<在那种情况下强> foo()。

Java 接口也是如此。如果两个接口 IA IB ,都声明方法 onShutter(),并且您有一个实现 IA ,您不能在需要实现 IB 的对象的上下文中使用它。

不幸的是,这是用于Android的Google相机API。它们定义了两个类似的接口ShutterCallbackShutterCallback。他们甚至有相同的名字,但他们是不同的。第一个全名实际上是 android.hardware.Camera.ShutterCallback ,后者 com.google.android.gms.vision.CameraSource.ShutterCallback

第一个界面是已弃用的 Android Camera API的一部分。如果您定位到Android平台21(Lollipop)及更高版本,Google建议您使用新的camera2 API。

第二个界面是适用于Android的vision Google API的一部分。此API不适用于完整的相机功能。它为一些与摄像机相关的检测任务提供了简单的包装,例如:条形码或面部检测。

您的代码不使用此类检测器。请注意,愿景 API不会暴露许多相机调整控件,例如它确实允许你捕捉图片,但是does not allow you to choose high picture resolution。当您切换到 camera2 API时,您将获得更多控制和更好的性能(您可以从官方basic sample开始)。

另一方面,您可以找到CameraSource.java并根据需要进行修改。

要让您的代码对代码进行最少的更改,为了让它能够正常工作, CameraSource.ShutterCallback 应该重命名为 Camera.ShutterCallback ;同样适用于 PictureCallback s。

你可能有

import com.google.android.gms.vision.CameraSource;

错误。

答案 2 :(得分:0)

是的,我可以修复,

我有同样的错误, 你需要导入正确的相机类(hardware.camera),你实现了图形类的原因。

删除Camera Obj并重新编写并选择hardware.camera。