简体   繁体   中英

Android camera preview tutorial

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 :)

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);
}
}

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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM