Android calls the camera screenshot capture demo without previewing a photo storage

Source: Internet
Author: User

The following tool class is used to simplify and optimize the common Photo Taking methods to achieve no preview.
 
Import java. io. file; import java. io. fileOutputStream; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import android. annotation. suppressLint; import android. annotation. targetApi; import android. app. activity; import android. content. context; import android. graphics. bitmap; import android. graphics. bitmapFactory; import android. graphics. canvas; import android. graphics. matrix; I Mport android. graphics. bitmap. compressFormat; import android. graphics. bitmap. config; import android. hardware. camera; import android. hardware. camera. pictureCallback; import android. media. audioManager; import android. OS. build; import android. util. log; import android. view. surfaceHolder; import android. view. surfaceView; import android. view. surfaceHolder. callback; import com.cn. zongyi. safebox. constants; import co M.cn. zongyi. safebox. myApplication; import com.cn. zongyi. safebox. ui. newBuildSafeBoxActivity; import com.cn. zongyi. safebox. ui. r; public class CameraUtil {private SurfaceView sView; // canvas view private SurfaceHolder surfaceHolder; // canvas Holderpublic Camera camera; // defines the Camera used by the System private boolean isPreview = false; // private AudioManager manager during browsing; // audio management private int volumn; // audio value private String picPath = ""; priv Ate NewBuildSafeBoxActivity context; public CameraUtil (Context context) {this. context = (NewBuildSafeBoxActivity) context;} @ SuppressWarnings ("deprecation") public void initCameraFirst () {manager = (AudioManager) context. getSystemService (Context. AUDIO_SERVICE); manager. setStreamMute (AudioManager. STREAM_SYSTEM, false); volumn = manager. getStreamVolume (AudioManager. STREAM_SYSTEM); if (volumn! = 0) {// If You Need to mute the image and it is not currently muted (muteMode settings can be placed in Preference) manager. setStreamVolume (AudioManager. STREAM_SYSTEM, 0, AudioManager. FLAG_REMOVE_SOUND_AND_VIBRATE);} sView = (SurfaceView) (Activity) context ). findViewById (R. id. sView); if (MyApplication. invadeMonitor () {// obtain SurfaceHoldersurfaceHolder = sView of SurfaceView. getHolder (); // Add a callback listener surfaceHolder for surfaceHolder. addCallback (new Callback () {public void surf AceChanged (SurfaceHolder holder, int format, int width, int height) {} public void surfaceCreated (SurfaceHolder holder) {// enable camera initCamera () When surface is created ();} // when the surface is destroyed, release the camera public void surfaceDestroyed (SurfaceHolder holder) {// if the camera is not null, release the camera if (camera! = Null) {// 7. when the program ends, call the StopPriview () of Camera to end the preview of the scenario and call the release () method to release the resource. if (isPreview) camera. stopPreview (); camera. release (); camera = null ;}}); // set this SurfaceView to not maintain the buffer surfaceHolder. setType (SurfaceHolder. SURFACE_TYPE_PUSH_BUFFERS) ;}@ TargetApi (Build. VERSION_CODES.GINGERBREAD) @ SuppressLint ("NewApi") private void initCamera () {if (! IsPreview) {int cameraCount = 0; Camera. cameraInfo cameraInfo = new Camera. cameraInfo (); cameraCount = Camera. getNumberOfCameras (); // get cameras numberif (cameraCount = 1) {// camera = Camera. open (); // call the open () method of Camera to open the Camera. Log. e ("TAG", "no front camera");} else {Log. e ("TAG", "__________ 4"); for (int camIdx = 0; camIdx <cameraCount; camIdx ++) {Camera. getCameraInfo (camIdx, cameraInfo); // get camerainfoif (cameraInfo. facing = Camera. cameraInfo. CAMERA_FACING_FRONT) {// specifies the orientation of the camera. Currently, the two defined values are CAMERA_FACING_FRONT front and CAMERA_FACING_BACK. try {Camera = camera. open (camIdx); // call the open () method of Camera to open the Camera .} Catch (RuntimeException e) {e. printStackTrace () ;}}} if (camera! = Null &&! IsPreview) {try {camera. setPreviewDisplay (surfaceHolder);} catch (IOException e) {e. printStackTrace ();} // start previewing camera. startPreview (); isPreview = true;} public PictureCallback my‑callback = new PictureCallback () {@ SuppressLint ("SimpleDateFormat") public void onPictureTaken (byte [] data, Camera camera) {Log. e ("TAG", "photo taken"); // reset the Sound manager. setStreamVolume (AudioManager. STREAM_SYSTEM, volumn, AudioManager. FLAG_ALLOW_RINGER_MODES); // creates a Bitmap (final bitmap) Bitmap = BitmapFactory based on the data obtained from the photo. decodeByteArray (data, 0, data. length); if (ExistSDCard () {picPath = Constants. HEAD_PIC_PATH + new SimpleDateFormat ("yyyyMMdd_HHmmss "). format (new Date () + ". jpg "; File file = new File (picPath); FileOutputStream outStream = null; try {// open the output stream outStream of the specified file = new FileOutputStream (File ); // output the Bitmap to the specified file. Matrix matrix = new Matrix (); Bitmap bm = Bitmap. createBitmap (batch, 1200 * bitmap. getHeight ()/bitmap. getWidth (), Config. ARGB_8888); // fixed the size of the photo matrix. setScale (float) bm. getWidth ()/(float) bitmap. getWidth (), (float) bm. getHeight ()/(float) bitmap. getHeight (); // note that the parameter must be float. Canvas canvas = new Canvas (bm); // you can use bm to create a canvas. // you can add a Canvas to bm. drawBitmap (bitmap, matrix, null); bm. compress (CompressFormat. JPEG, 40, outStream); outStream. close ();} catch (IOException e) {e. printStackTrace () ;}} else {Log. e ("TAG", "SD card unavailable");} camera. stopPreview (); camera. startPreview (); isPreview = true; MyApplication. getInstance (). setUser_pic_path (picPath); context. cameraRefresh (picPath) ;}}; public static boolean ExistSDCard () {if (android. OS. environment. getExternalStorageState (). equals (android. OS. environment. MEDIA_MOUNTED) {return true;} elsereturn false ;}}


View the layout File


Xmlns: tools = "http://schemas.android.com/tools"
Android: id = "@ + id/new_build_resize_layout"
Android: background = "# ffffff"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">



Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: orientation = "vertical"
Android: paddingLeft = "10dp"
Android: paddingRight = "10dp"
Tools: ignore = "UselessParent">



Android: layout_width = "fill_parent"
Android: layout_height = "50dp">

Android: id = "@ + id/sView"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"/>

Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: background = "# ffffff"/>




Android: id = "@ + id/et_pwd"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: EMS = "10"
Android: hint = "@ string/please_enter_a_password"
Android: inputType = "number"
Android: maxLength = "@ integer/edit_length"
Android: password = "true"
Tools: ignore = "Deprecated">






Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">


Android: id = "@ + id/ckb_show_pwd"
Android: layout_width = "0dp"
Android: layout_height = "wrap_content"
Android: layout_weight = "1"
Android: text = "@ string/show_pwd"/>




Android: id = "@ + id/next_step_linear"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: layout_marginBottom = "50dp"
Android: layout_marginLeft = "30dp"
Android: layout_marginRight = "30dp">


Android: id = "@ + id/next_step"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/new_build_safe_box_new_step_button"/>





 
 

Related Article

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.