Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of taking pictures without preview in Android

2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "how to achieve photo function without preview in Android". The editor shows you the operation process through an actual case. The method of operation is simple and fast, and it is practical. I hope this article "how to achieve photo function without preview in Android" can help you solve the problem.

The code is as follows:

Public void onTakePhotoClicked () {final SurfaceView preview = new SurfaceView (this); SurfaceHolder holder = preview.getHolder (); / / deprecated setting, but required on Android versions prior to 3.0holder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); holder.addCallback (new SurfaceHolder.Callback () {@ Override / / The preview must happen at or after this point or takePicture fails public void surfaceCreated (SurfaceHolder holder) {Log.d (TAG, "Surface created"); camera = null Try {camera = Camera.open (Camera.CameraInfo.CAMERA_FACING_BACK); Log.d (TAG, "Opened camera"); try {camera.setPreviewDisplay (holder);} catch (IOException e) {throw new RuntimeException (e);} camera.startPreview (); Log.d (TAG, "Started preview") / / delay ThreadUtils.postOnUiThreadDelayed (new Runnable () {@ Override public void run () {Log.e ("zgj", "start shooting"); camera.takePicture (null, null, CameraService.this);}}, 5000) } catch (Exception e) {if (camera! = null) camera.release (); throw new RuntimeException (e);} @ Override public void surfaceDestroyed (SurfaceHolder holder) {} @ Override public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {}}; WindowManager wm = (WindowManager) getSystemService (Context.WINDOW_SERVICE) WindowManager.LayoutParams params = new WindowManager.LayoutParams (1, 1, / Must be at least 1x1 WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 0, / Don't know if this is a safe default PixelFormat.UNKNOWN); / / Don't set the preview visibility to GONE or INVISIBLE wm.addView (preview, params);} @ Override public void onPictureTaken (byte [] bytes, Camera camera) {Log.e ("zgj", "photo end") File pictureDir = Environment.getExternalStorageDirectory (); if (pictureDir = = null) {Log.d ("zgj", "Error creating media file, check storage permissions!"); return;} try {String pictureName = "ssss.png"; File file = new File (pictureDir + "/ pic/"); if (! file.exists ()) {file.mkdir ();} file = new File (pictureDir + "/ pic/" + pictureName) FileOutputStream fos = new FileOutputStream (file); fos.write (bytes); fos.close ();} catch (FileNotFoundException e) {Log.d ("zgj", "File not found:" + e.getMessage ());} catch (IOException e) {Log.d ("zgj", "Error accessing file:" + e.getMessage ()) }} this is the end of the content about "how to achieve the function of taking pictures without preview in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report