Implementation of the Android mobile phone photo function

Source: Internet
Author: User

Implementation of the Android mobile phone photo function

1. layout file main. xml


  

Ii. MainActivity. java

Import java. io. file; import java. io. fileOutputStream; import android. app. activity; import android. hardware. camera; import android. hardware. camera. pictureCallback; import android. OS. bundle; import android. OS. environment; import android. view. motionEvent; import android. view. surfaceHolder; import android. view. surfaceHolder. callback; import android. view. surfaceView; import android. view. view; import android. view. vi EwGroup; import android. view. window; import android. view. windowManager; public class MainActivity extends Activity {private View layout; private Camera camera; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FU LLSCREEN); setContentView (R. layout. main); layout = this. findViewById (R. id. buttonlayout); SurfaceView surfaceView = (SurfaceView) this. findViewById (R. id. surfaceView); surfaceView. getHolder (). setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS); surfaceView. getHolder (). setFixedSize (176,144); surfaceView. getHolder (). setKeepScreenOn (true); surfaceView. getHolder (). addCallback (new SurfaceCallback ();} p Ublic void takepicture (View v) {if (camera! = Null) {switch (v. getId () {case R. id. takepicture: camera. takePicture (null, null, new MyPictureCallback (); break; case R. id. autofocus: camera. autoFocus (null); break ;}} private final class MyPictureCallback implements PictureCallback {public void onPictureTaken (byte [] data, Camera camera) {try {File jpgFile = new File (Environment. getExternalStorageDirectory (), System. currentTimeMillis () +. Jpg); FileOutputStream outStream = new FileOutputStream (jpgFile); outStream. write (data); outStream. close (); camera. startPreview ();} catch (Exception e) {e. printStackTrace () ;}} private final class SurfaceCallback implements Callback {public void surfaceCreated (SurfaceHolder holder) {try {camera = Camera. open (); // open the Camera. parameters parameters = camera. getParameters (); // Log. I (Main Activity, parameters. flatten (); parameters. setPreviewSize (800,480); parameters. setPreviewFrameRate (5); parameters. setPictureSize (1024,768); parameters. setJpegQuality (80); camera. setParameters (parameters); camera. setPreviewDisplay (holder); camera. startPreview (); // start preview} catch (Exception e) {e. printStackTrace () ;}} public void surfaceChanged (SurfaceHolder holder, int format, int width, int hei Ght) {} public void surfaceDestroyed (SurfaceHolder holder) {if (camera! = Null) {camera. release (); camera = null ;}}@ Override public boolean onTouchEvent (MotionEvent event) {if (event. getAction () = MotionEvent. ACTION_DOWN) {layout. setVisibility (ViewGroup. VISIBLE); return true;} return super. onTouchEvent (event );}}

3. Add Permissions

 
      
       
       
        
    
   
  

The effect is as follows:

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.