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

Android how to customize Camera to realize the small function of taking pictures

2025-03-31 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 customize Android Camera to achieve small photo function". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to customize Android Camera to achieve photo mini function" can help you solve the problem.

First of all, implement a custom photo function.

Custom layout

Initialize the control:

SurfaceView = (SurfaceView) findViewById (R.id.surface); holder = surfaceView.getHolder (); holder.addCallback (this); holder.setType (SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); btn.setOnCLickListener (new OnClickLister (View v) {if (mCamera = = null) {mCamera = Camera.open ();} mCamera.takePicture (null,null,this);}); @ Overridepublic void surfaceCreated (SurfaceHolder surfaceHolder) {initStartCamera (surfaceHolder) } @ Overridepublic void surfaceChanged (SurfaceHolder surfaceHolder, int I, int i1, int i2) {mCamera.autoFocus (new Camera.AutoFocusCallback () {@ Overridepublic void onAutoFocus (boolean success, Camera camera) {isAutoFocus = success; initCameraParams (); mCamera.cancelAutoFocus (); mCamera.startPreview ();}}) } @ Override public void surfaceDestroyed (SurfaceHolder surfaceHolder) {/ / release hardware / / releaseCamera () when holder is reclaimed;} @ Override protected void onPause () {super.onPause (); releaseCameraSource ();} @ Override protected void onResume () {super.onResume () / / TODO: check to see if there is a black screen when exiting to other pages if (surfaceView! = null) {surfaceView.postDelayed (new Runnable () {@ Override public void run () {initCameraParams ();}}, 50) }} private void initStartCamera (SurfaceHolder surfaceHolder) {try {mCamera = Camera.open (); mCamera.setDisplayOrientation (90); mCamera.setPreviewDisplay (surfaceHolder); mCamera.startPreview ();} catch (IOException e) {e.printStackTrace () }} private void initCameraParams () {if (mCamera! = null) {Camera.Parameters parameters = mCamera.getParameters (); parameters.setPictureFormat (ImageFormat.JPEG); parameters.setJpegQuality (90); List supportedPictureSizes = parameters.getSupportedPictureSizes (); WindowManager manager = (WindowManager) getSystemService (WINDOW_SERVICE); Display display = manager.getDefaultDisplay () Point point = new Point (); display.getSize (point); int screenWidth = point.x; int screenHeight = point.y; / / find the appropriate size of the picture if (supportedPictureSizes! = null & &! supportedPictureSizes.isEmpty ()) {int screenSize = screenHeight * screenWidth; Camera.Size picSize = null For (Camera.Size size: supportedPictureSizes) {int value = size.height * size.width; if (value picSize.width * picSize.height) {picSize = size }} if (picSize = = null) {picSize = supportedPictureSizes.get (0);} parameters.setPictureSize (picSize.width, picSize.height) } / / set focus mode List supportedFocusModes = parameters.getSupportedFocusModes (); if (supportedFocusModes.contains (Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) {/ / Quick focus parameters.setFocusMode (Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE) } else {parameters.setFocusMode (Camera.Parameters.FLASH_MODE_AUTO);} try {mCamera.setParameters (parameters); mCamera.startPreview ();} catch (Exception e) {e.printStackTrace () } private void releaseCameraSource () {if (mCamera! = null) {mCamera.setPreviewCallback (null); mCamera.stopPreview (); mCamera.release (); mCamera = null;}}

Call the camera's shooting function:

Click to take a photo to call camera.takePicture (null,null,this)

Get the image data returned from the callback of the photo.

Public void onPictureTaken (final byte [] bytes,final Camera camera) {/ / take pictures of the image data that fell back. Final String filePath = Environment.getExternalStorageDirectory (). GetPath () + "/ DCIM/Camera/"; final String picturePath = System.currentTimeMillis () + ".jpg"; final File file = new File (filePath, picturePath); new Thread (new Runnable () {@ Override public void run () {Bitmap bitmap = BitmapFactory.decodeByteArray (bytes, 0, bytes.length); bitmap = rotateBitmapByDegree (bitmap, 90) BufferedOutputStream bos = null; try {/ / prevent photos from being compressed bos = new BufferedOutputStream (new FileOutputStream (file)); bitmap.compress (Bitmap.CompressFormat.JPEG, 100,100, bos); bos.flush (); bos.close () Bitmap.recycle (); Intent intent = new Intent (TakePhotoActivity.this,TPreViewPicActivity.class); intent.putExtra ("filePath", filePath); intent.putExtra ("picturePath", picturePath); startActivityForResult (intent,102);} catch (FileNotFoundException E1) {e1.printStackTrace () } catch (IOException E1) {e1.printStackTrace ();}) .start ();}

The next thing we're going to talk about is the problem of saving images being rotated as we mentioned above:

Public Bitmap rotateBitmapByDegree (Bitmap bm,int degree) {Bitmap bitmap; Matrix matrix = new Matrix (); matrix.postRotate (degree); try {bitmap = Bitmap.createBitmap (bm,0,bm.getWidth,bm.getHeight,matrix,true);} catch (OutOfMemoryError e) {e.printStackTrace ();} if (bitmap = = null) {bitmap = bm;} if (bm! = bitmap) {bm.recycle ();} return bitmap;} @ Overridepublic void onPause () {super.onPause () If (camera! = null) {if (isPrevew) {camera.stopPreview (); camera.release (); camera= null; isPreView= false;}} @ Override protected void onResume () {super.onResume (); openCamera ();}

* # in addition, the permission problem of android 6.0may cause the first time to enter the photo interface with a black screen. The solution requests permission before the photo interface.

Finally, the request permission code is attached:

Public void checkPermission () {if (Build.VERSION.SDK_INT > = Build.VERSION_CODES.M) {requestPermissions (new String [] {Manifest.permission.CAMERA}, new TCallPhoneTool.PermissionListener () {@ Override public void onGranted () {/ / openCamera () } @ Override public void onRefused (List deniedPermissions) {showMissingPermissionDialog ();}});} else {/ / openCamera ();}} TCallPhoneTool.PermissionListener mListener Final int REQUEST_CODE_STORAGE = 131; public void requestPermissions (String [] permissions, TCallPhoneTool.PermissionListener listener) {List deniedPermissions = new ArrayList (); mListener = listener; for (String permission: permissions) {if (ContextCompat.checkSelfPermission (this,permission) = = PackageManager.PERMISSION_DENIED) {deniedPermissions.add (permission) }} if (deniedPermissions.size () > 0) {ActivityCompat.requestPermissions (this,deniedPermissions.toArray (new String [deniedPermissions.size ()]), REQUEST_CODE_STORAGE);} else {mListener.onGranted ();}} public void showMissingPermissionDialog () {android.support.v7.app.AlertDialog.Builder builder = new android.support.v7.app.AlertDialog.Builder (this) Builder.setTitle (getString (com.to8to.baselib.R.string.tip_permision_miss)); builder.setNegativeButton ("cancel", new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {finish ();}}) Builder.setPositiveButton (Settings, new DialogInterface.OnClickListener () {@ Override public void onClick (DialogInterface dialog, int which) {startAppSetting ();}}); builder.setCancelable (false); builder.show (); public void startAppSetting () {Intent intent = new Intent (Settings.ACTION_APPLICATION_DETAILS_SETTINGS) Intent.setData (Uri.parse ("package:" + this.getPackageName ()); startActivity (intent);} this is the end of the content about "how to customize Camera to take photos 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