Two ways to use the camera in Android (original)

Source: Internet
Author: User

The camera on the phone is needed for social applications or for scanning QR codes.

There are two main ways to enable this hardware in your program

1. Send the camera application with the intent boot system

The intent-filter in the androidmanifest of this app are as follows

< Intent-filter >                <  android:name= "Android.media.action.VIDEO_CAPTURE"/>                <  android:name= "Android.intent.category.DEFAULT"/></  intent-filter>

The code to send intent is as follows:

Intent Intent = new Intent ("Android.media.action.IMAGE_CAPTURE");                String Path = "Storage/sdcard1/picture1.jpg";        File pic =new file (path);        Uri uri=      uri.fromfile (pic);        Set Photo save location        Intent.putextra (Mediastore.extra_output, URI) after taking photos;            StartActivity (Intent);

2. Call the API yourself, try the camera object to complete the shooting save

The code is as follows:

    @Override protected void Onresume () {//TODO auto-generated Method Stub super.onresume ();        Surfaceview = (Surfaceview) Findviewbyid (R.ID.SV);        Get the preview screen of the receiving class Surfaceholder Holder = Surfaceview.getholder ();        The Receive class settings callback-->surface created when the camera starts previewing, and the data is closed//set when exiting to originate from the rendering engine, without using its own buffer.        Holder.settype (surfaceholder.surface_type_push_buffers);        Camera = Camera.open ();                Holder.addcallback (New Callback () {@Override public void surfacedestroyed (Surfaceholder holder) { TODO auto-generated Method Stub Camera.stoppreview ();            Stop Camera preview camera.release ();  } @Override public void surfacecreated (Surfaceholder holder) {//TODO auto-generated                Method stub//camera using try {camera.setpreviewdisplay (holder);                    } catch (IOException e) {TODO auto-generated Catch block E.printstacktrace (); }//Settings Preview display Camera.startpreview ();//Start preview} @Override public void Surfacech Anged (surfaceholder holder, int format, int width, int height) {//TODO auto-generated    Method stub}});    public void Click (View v) {shoot (); } public void Shoot () {camera.takepicture (null, NULL, new Picturecallback () {@Override P ublic void Onpicturetaken (byte[] data, Camera camera) {//TODO auto-generated method stub B                Itmap bitmap = bitmapfactory.decodebytearray (data, 0, data.length);                FileOutputStream fos = null;                    try {fos = new FileOutputStream ("sdcard/pic1.jpg"); Boolean issuccess = Bitmap.compress (Compressformat.jpeg, A, fos);                    if (issuccess) {Toast.maketext (mainactivity.this, "shooting and Storage succeeded", 0). Show ();                    } else {Toast.maketext (mainactivity.this, "shooting and storage Failed", 0). Show ();                }} catch (Exception e) {e.printstacktrace ();                        } finally {if (FOS! = null) {try {fos.close ();                            } catch (IOException e) {//TODO auto-generated catch block                        E.printstacktrace ();        }                    }                }            }        });        Intent Intent = new Intent ();        Intent.setaction (intent.action_media_mounted);        Intent.setdata (Uri.fromfile ("New File") ("/sdcard"));        Sendbroadcast (Intent);    Camera.startpreview (); }

Comparison of the two methods

The previous method needs to jump out of the program and is less flexible, but easy to use and less code-friendly and maintainable.

The latter approach is complex, but it is possible to customize the style of the activity and the function options, and in this application can complete the shooting function, the use of a lot.

Two ways to use the camera in Android (original)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.