Android camera preview tutorial

44,095

Solution 1

Below Tutorials will help you.

http://www.vogella.com/articles/AndroidCamera/article.html

http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

Call inbuilt camera intent to have picture.

public class demo extends Activity {

Button ButtonClick;
int CAMERA_PIC_REQUEST = 1337; 


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ButtonClick =(Button) findViewById(R.id.Camera);
    ButtonClick.setOnClickListener(new OnClickListener (){
        @Override
        public void onClick(View view)
        {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            // request code

            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if( requestCode == 1337)
    {
    //  data.getExtras()
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

          Now you have received the bitmap..you can pass that bitmap to other activity
          and play with it in this activity or pass this bitmap to other activity
          and then upload it to server.
    }
    else 
    {
        Toast.makeText(demo.this, "Picture NOt taken", Toast.LENGTH_LONG);
    }
    super.onActivityResult(requestCode, resultCode, data);
}
}

Solution 2

I'm currently working on a fork of the CameraPreviewSample project. The nice thing about this example is that the github sources are tagged for several steps needed to make the camera preview work.

So if you're looking into that running over the several tags (check readme for details) might be a good idea.

Another good resource are the training articles from Google. For camera the Android Training Article about Camera control is best.

Solution 3

Using SurfaceView or TextureView directly is not recommended, use PreviewView instead.

The PreviewView, part of the CameraX Jetpack library, makes displaying a camera preview easier for developers by providing a developer-friendly, consistent, and stable API across a wide range of Android devices.

In the xml

<androidx.camera.view.PreviewView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:scaleType="fitCenter" />

In your code,

// Create a preview use case instance
val preview = Preview.Builder().build()

//camera selector
val cameraSelector : CameraSelector = CameraSelector.Builder()
      .requireLensFacing(CameraSelector.LENS_FACING_BACK)
      .build()


// Bind the preview use case and other needed user cases to a lifecycle
val camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview)

// Create a surfaceProvider using the bound camera's cameraInfo
val surfaceProvider = previewView.createSurfaceProvider(camera.cameraInfo)

// Attach the surfaceProvider to the preview use case to start preview
preview.setSurfaceProvider(surfaceProvider)

See the complete documentation here.

Share:
44,095
Lasse
Author by

Lasse

Updated on July 05, 2022

Comments

  • Lasse
    Lasse almost 2 years

    I have a litte problem with a tutorial that I follow. I want to make a android application with a camera preview, but until now I haven't found any good tutorial that show how to do it. Here is the link The tutorial I'm not quite shure if I can use the "camera with intent" insted of the "camera preveiew" ? What do I do.

    Thanks :)

  • Lasse
    Lasse about 12 years
    Ohh THANKS SO MUCH :) You just saved my day :)
  • Vipul
    Vipul about 12 years
    If you are satisfied with the answer then Accept it as it will help others. :)
  • Lasse
    Lasse about 12 years
    But, is there any way not to use the front facing camera and then use the normal camera ?
  • Vipul
    Vipul about 12 years
    Can you be clear on what exactly you want to do in your application?
  • Lasse
    Lasse about 12 years
    I want to have a camera part as a part of my app. As you take a picture with the camera, it switches activity to a screen where you can write some data to the picture and then upload it to a database :)
  • Lasse
    Lasse about 12 years
    That's nice :) Thanks, but can I use that with a the camera preview ?
  • Vipul
    Vipul about 12 years
    It will launch camera application where you will have preview.once you click capture button it will capture snap and will return to you in Bitmap format :)
  • Lasse
    Lasse about 12 years
    So I just have to take the preview class from the first tutorial, and implement the code to it ?
  • Lasse
    Lasse about 12 years
    I have now figured out how to solve my problem :) But I still have a question though. What is the ID or name of the bitmap image that i have captured ? And how do I if necessary change that ?
  • brousch
    brousch almost 10 years
    Unfortunately there is no license on this code, so I don't know how re-usable it is.
  • tony gil
    tony gil about 8 years
    lars vogel, aka vogella, is a consistent source for sample code. UPVOTED! :) tks @VipulShah