3

I got an error when I implemented this code below. I got an error on CameraActivity.java (please see my picture attachment) anyone can fix this?

this is 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

This is 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>

this is 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>

Hope anyone can fix this code... I compile with android studio 3.0.1

zlz
  • 33
  • 7

3 Answers3

0

Try adding:

import android.hardware.Camera;
sajas
  • 1,599
  • 1
  • 17
  • 39
Aan
  • 1
0

Let's start with the basic statement: Java is not a duck-type language. This means that if two classes, A and B, both define a method foo(), you cannot use an object of class B in the context that expects object of class A, even if you only execute foo() in that context.

Same is true for Java interfaces. If two interfaces, IA and IB, both declare method onShutter(), and you have a class that implements IA, you cannot use it in context that expects objects that implements IB.

This is what, unfortunately, happened with Google camera APIs for Android. They define two similar interfaces, ShutterCallback and ShutterCallback. They even have the same names, but they are different. The first full name is actually android.hardware.Camera.ShutterCallback, the latter com.google.android.gms.vision.CameraSource.ShutterCallback.

The first interface is part of the deprecated Android Camera API. If you target Android platforms 21 (Lollipop) and higher, Google recommends to use the new camera2 API.

The second interface is part of a vision Google APIs for Android. This API is not intended to cover full Camera functionality. It provides easy wrappers for some camera-related detection tasks, e.g. barcode, or face detection.

Your code does not employ such detectors. Please note that the vision API does not expose many camera tuning controls, e.g. it does allow you to capture a picture, but does not allow you to choose high picture resolution. You will get even more control, and better performance, when you switch to camera2 API (you can start with the official basic sample).

On the other hand, you can find the CameraSource.java and modify it as you wish.

To let your code with minimal change to your code, to let it work, CameraSource.ShutterCallback should be renamed to Camera.ShutterCallback; same for both PictureCallbacks.

You probably have

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

by mistake.

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • can you tell more detail please – zlz Jan 20 '18 at 16:44
  • Let me make the short story long. See the updated answer. – Alex Cohn Jan 21 '18 at 09:59
  • http://i293.photobucket.com/albums/mm60/zalzondabuzz/Untitled_zpsb2uonfmy.png here's an eror – zlz Jan 27 '18 at 18:42
  • Oh yeah , this is a source code, before I implemented into android studio,,, may be you can give me a fix code on android studio... http://saurabhsharma123k.blogspot.co.id/2013/05/set-live-frame-on-camera-capturing.html – zlz Jan 27 '18 at 18:57
  • sorry, I don't understand your question now – Alex Cohn Jan 27 '18 at 22:28
  • I mean, I got that source from this blog : http://saurabhsharma123k.blogspot.co.id/2013/05/set-live-frame-on-camera-capturing.html – zlz Jan 28 '18 at 04:18
  • I mean, I got that source from this blog : http://saurabhsharma123k.blogspot.co.id/2013/05/set-live-frame-on-camera-capturing.html can you help me to fix that source code – zlz Jan 28 '18 at 05:18
  • I don't understand how that blog is related to your original question. And I don't know what fixes you expect for that code, I believe it works as is. – Alex Cohn Jan 28 '18 at 07:23
0

Yes I can Fix,

I have Same Error, You Need to Import Right Camera Class(hardware.camera) , You implement Graphics class that's why.

Remove Camera Obj and re write And Select hardware.camera.