In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge of this article "how to use a camera in Android", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use a camera in Android" article.
An example Application of Android camera
SurfaceHolder.Callback
Public class MyCameraDemo extends Activity {private SurfaceView surface = null; private Button but = null; private SurfaceHolder holder = null; private Camera cam = null; private boolean previewRunning = true; @ Override public void onCreate (Bundle savedInstanceState) {super.onCreate (savedInstanceState); super.setContentView (R.layout.main); this.but = (Button) super.findViewById (R.id.but); this.surface = (SurfaceView) super.findViewById (R.id.surface) This.holder = this.surface.getHolder (); this.holder.addCallback (new MySurfaceViewCallback ()); / / set buffer type this.holder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); / / set resolution this.holder.setFixedSize (600,350); this.but.setOnClickListener (new OnClickListenerImpl ()) } private class OnClickListenerImpl implements OnClickListener {@ Override public void onClick (View v) {if (MyCameraDemo.this.cam! = null) {/ / autofocus MyCameraDemo.this.cam.autoFocus (new AutoFocusCallbackImpl ()) } private class MySurfaceViewCallback implements SurfaceHolder.Callback {/ / when the format of the preview interface changes, call public void surfaceChanged (SurfaceHolder holder, int format, int width, int height) {} / / the first instantiation interface calls public void surfaceCreated (SurfaceHolder holder) {MyCameraDemo.this.cam = Camera.open (0) / / get the first camera / / window service WindowManager manager = (WindowManager) MyCameraDemo.this .getSystemService (Context.WINDOW_SERVICE); / / get the display display object Display display = manager.getDefaultDisplay (); / / camera parameter Parameters param = MyCameraDemo.this.cam.getParameters () / / set the camera preview size to display size param.setPreviewSize (display.getWidth (), display.getHeight ()); param.setPreviewFrameRate (5); / / 5 frames per second param.setPictureFormat (PixelFormat.JPEG); / / Image format param.set ("jpen-quality", 80); / / Image quality, up to 100 MyCameraDemo.this.cam.setParameters (param) Try {MyCameraDemo.this.cam.setPreviewDisplay (MyCameraDemo.this.holder);} catch (IOException e) {} MyCameraDemo.this.cam.startPreview (); / / Preview MyCameraDemo.this.previewRunning = true / / Preview} @ Override public void surfaceDestroyed (SurfaceHolder holder) {if (MyCameraDemo.this.cam! = null) {if (MyCameraDemo.this.previewRunning) {MyCameraDemo.this.cam.stopPreview (); / / stop previewing MyCameraDemo.this.previewRunning = false;} MyCameraDemo.this.cam.release () } private class AutoFocusCallbackImpl implements AutoFocusCallback {@ Override public void onAutoFocus (boolean success, Camera camera) {if (success) {/ / successful MyCameraDemo.this.cam.takePicture (sc, pc, jpgcall) } private PictureCallback jpgcall = new PictureCallback () {@ Override public void onPictureTaken (byte [] data, Camera camera) {/ / the operation of saving the picture Bitmap bmp = BitmapFactory.decodeByteArray (data, 0, data.length) String fileName = Environment.getExternalStorageDirectory () .toString () + File.separator + "mldnphoto" + File.separator + "MLDN_" + System.currentTimeMillis () + ".jpg"; File file = new File (fileName); if (! file.getParentFile () .exists ()) {file.getParentFile () .jpg () / / create a folder} try {BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (file)); bmp.compress (Bitmap.CompressFormat.JPEG, 80, bos); / / compress images into the buffer bos.flush (); bos.close () Toast.makeText (MyCameraDemo.this, "photo taken successfully, the photo has been saved in the" + fileName + "file! , Toast.LENGTH_SHORT) .show ();} catch (Exception e) {Toast.makeText (MyCameraDemo.this, "photo failed!" , Toast.LENGTH_SHORT) .show ();} MyCameraDemo.this.cam.stopPreview (); MyCameraDemo.this.cam.startPreview ();}}; private ShutterCallback sc = new ShutterCallback () {@ Override public void onShutter () {/ / actions taken after the shutter is pressed}} Private PictureCallback pc = new PictureCallback () {@ Override public void onPictureTaken (byte [] data, Camera camera) {}};}
Main layout function
The above is about the content of this article on "how to use a camera in Android". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.