SoFunction
Updated on 2025-03-10

Android implements simple photography functions

A simple photography function, after taking a photo, the picture taken is displayed in another activit.
First is the layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    xmlns:app="/apk/res-auto"
    xmlns:tools="/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <SurfaceView
        android:
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <Button
        android:
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Photograph"></Button>

</LinearLayout>

A SurfaceView presents the camera's image;
button is the function of taking photos after clicking;

  • Initialize a SurfaceView control;
sf = findViewById();
        ().addCallback(new () {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                stop();
            }
        }

A brief explanation of the connection between Surface.

Surface is an important element of Android, used for graphic drawing of Android screens. SurfaceView is an inheritance class of the view (View), and each SurfaceView encapsulates a Surface inline. By calling SurfaceHolder, you can call the SurfaceView to control the size and size of the figure. SurfaceHolder is obtained through getholder(). After creating the SurfaceHolder object, use() to call back the SurfaceHolder to control the SurfaceView.

surfaceCreated This function will be called immediately after the first creation of the Surface. Programs can do some initialization work related to drawing interfaces in this function. Generally, they draw the interfaces in another thread, so do not draw Surfaces in this function.

surfaceChanged This function will be called when the state (size and format) of the Surface changes. After surfaceCreated is called, the function will be called at least once.

surfaceDestroyed The function will be called before the Surface is destroyed. After the function is called, the Surface cannot be continued. Generally, the resource used is cleaned in this function.

Create camera object (note that you need to use import; this package)

public void start() {
 camera = ();
        try {
            (());
            ();//Start preview screen            (90);//The shooting screen rotates 90 degrees        } catch (IOException e) {
            ();
        }
    }

Set the SurfaceHolder object you just created into camera;
The above steps are called in the surfaceCreated() method;

Release camera resources at the end of the interface:

public void stop() {
        ();
        ();
    }

Steps to follow after clicking the photo button

findViewById().setOnClickListener(new () {
            @Override
            public void onClick(View v) {
                (null, null, new () {//Start take pictures;                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {//Calling back after shooting;                        String path = null;

                        if ((path = savephoto(data)) != null) {
                            Intent in = new Intent(, );
                            ("path", path);
                            startActivity(in);
                        } else {
                            (, "save photo fail", Toast.LENGTH_LONG).show();
                        }

                    }
                });
            }
        });

savephoto() saves the current photo resource into a temporary file;

private String savephoto(byte[] bytes) {
        try {
            File f = ("img", "");//Prefix, suffix            FileOutputStream fos = new FileOutputStream(f);
            (bytes);
            ();
            ();
            return ();
        } catch (IOException e) {
            ();
        }
        return null;
    }

Store binary data into a temporary file and return the file path;

After taking a photo, jump to another interface to display:

package ;

import ;
import ;
import ;
import ;
import ;

import ;

import ;

public class MyActivity extends Activity {
    private ImageView iv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        (savedInstanceState);
        iv=new ImageView();

        setContentView(iv);
        Intent intent =getIntent();
        String path=("path");
        if (path!=null){
            ((new File(path)));
        }
    }
}

((new File(path))); Create a file through the file path;

The code for the main activity is as follows:

package ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;

import ;

import ;
import ;
import ;


public class MainActivity extends AppCompatActivity {
    private SurfaceView sf;
    private Camera camera;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        (savedInstanceState);
        setContentView(.activity_main);
        findViewById().setOnClickListener(new () {
            @Override
            public void onClick(View v) {
                (null, null, new () {//Start take pictures;                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {//Calling back after shooting;                        String path = null;

                        if ((path = savephoto(data)) != null) {
                            Intent in = new Intent(, );
                            ("path", path);
                            startActivity(in);
                        } else {
                            (, "save photo fail", Toast.LENGTH_LONG).show();
                        }

                    }
                });
            }
        });


        sf = findViewById();
        ().addCallback(new () {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                start();
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                stop();
            }
        });

    }

    private String savephoto(byte[] bytes) {
        try {
            File f = ("img", "");//Prefix, suffix            FileOutputStream fos = new FileOutputStream(f);
            (bytes);
            ();
            ();
            return ();
        } catch (IOException e) {
            ();
        }
        return null;
    }

    public void start() {
        camera = ();
        try {
            (());
            ();//Start preview screen            (90);//The shooting screen rotates 90 degrees        } catch (IOException e) {
            ();
        }
    }

    public void stop() {
        ();
        ();
    }

}

Remember to add camera permissions:

<uses-permission android:name=""></uses-permission>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.