SoFunction
Updated on 2025-04-12

Android implements photo shooting function

Hello everyone, this is a simple photo-taking function, a very simple interface, a SurfaceView displays the image area and a "photo" button. Directly upload the code!

1. (Main interface)

package ; 
 
import ; 
import ; 
import ; 
import ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class CameraDemoActivity extends Activity{ 
 
 private final static String TAG = "CameraActivity"; 
 private SurfaceView surfaceView; 
 private SurfaceHolder surfaceHolder; 
 private Camera camera; 
 private File picture; 
 private Button btnSave; 
  
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  (savedInstanceState); 
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
  (Window.FEATURE_NO_TITLE); 
  setContentView(); 
  setupViews(); 
 } 
  
 private void setupViews(){ 
  surfaceView = (SurfaceView) findViewById(.camera_preview); // Camera interface to instantiate components 
  surfaceHolder = (); // Camera interface to instantiate components 
  (surfaceCallback); // Add a callback for the SurfaceHolder 
  (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
   
  btnSave = (Button) findViewById(.save_pic); 
   
  (new OnClickListener() { 
    
   @Override 
   public void onClick(View v) { 
    takePic(); 
   } 
  }); 
 } 
  
  
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event) { 
  if (keyCode == KeyEvent.KEYCODE_CAMERA 
    || keyCode == KeyEvent.KEYCODE_SEARCH) { 
   takePic(); 
   return true; 
  } 
  return (keyCode, event); 
 } 
 
 private void takePic() { 
 
  ();// stop the preview 
 
  (null, null, pictureCallback); // picture 
 } 
 
 // Photo call back 
  pictureCallback = new () { 
  //@Override 
  public void onPictureTaken(byte[] data, Camera camera) { 
   new SavePictureTask().execute(data); 
   (); 
  } 
 }; 
 
 // save pic 
 class SavePictureTask extends AsyncTask<byte[], String, String> { 
  @Override 
  protected String doInBackground(byte[]... params) { 
   String fname = ("yyyyMMddhhmmss", new Date()).toString()+".jpg"; 
    
   (TAG, "fname="+fname+";dir="+()); 
   //picture = new File((),fname);// create file 
    
   picture = new File(()+"/"+fname); 
    
   try { 
    FileOutputStream fos = new FileOutputStream(()); // Get file output stream 
    (params[0]); // Written to the file 
    (); 
   } catch (Exception e) { 
    (); 
   } 
   return null; 
  } 
 } 
 
 // SurfaceHodler Callback handle to open the camera, off camera and photo size changes 
  surfaceCallback = new () { 
 
  public void surfaceCreated(SurfaceHolder holder) { 
   (TAG, "surfaceCallback===="); 
   camera = (); // Turn on the camera 
   try { 
    (holder); // Set Preview 
   } catch (IOException e) { 
    ();// release camera 
    camera = null; 
   } 
  } 
 
  public void surfaceChanged(SurfaceHolder holder, int format, int width, 
    int height) { 
   (TAG,"====surfaceChanged"); 
    parameters = (); // Camera parameters to obtain 
   ();// Setting Picture Format 
//   ("rotation", 180); // Arbitrary rotation 
   (0); 
//   (400, 300); // Set Photo Size 
   (parameters); // Setting camera parameters 
   (); // Start Preview 
  } 
 
  public void surfaceDestroyed(SurfaceHolder holder) { 
   (TAG,"====surfaceDestroyed"); 
   ();// stop preview 
   (); // Release camera resources 
   camera = null; 
  } 
 }; 
} 

2. (Layout file)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:andro 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
 
 < 
  android: 
  android:layout_width="800dip" 
  android:layout_height="600dip" 
  android:layout_alignParentTop="true" 
  android:layout_centerInParent="true" 
  android:layout_gravity="center_vertical|center_horizontal" /> 
 
 <Button 
  android: 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/txt_save" /> 
 
</LinearLayout> 

3. Don't forget to add permissions:

<uses-permission android:name="" /> 
<uses-feature android:name="" /> 
<uses-feature android:name="" android:required="false" /> 

Source code download:Android implements photo shooting function

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.