Android 47: Do not preview photos and picture zoom clipping

Source: Internet
Author: User

Subtitle: Take picture without preview Android

Google has set a threshold for privacy protection, which is that it is necessary to have an image preview in the development of Android apps. This will secretly record the behavior and peripheral information of mobile phone users in the background of malicious programs such as Android flooding service. Such a threshold also includes the handset manufacturer's own camera software in the photo must have a voice, so as to avoid some candid situation.

In the technical research and the use of some special harmless scenes, we need to use the photo without preview. This article is based on this as a background to do some research. Just with not more than five phone test, do not guarantee that all the equipment is not a problem.

Disclaimer: This article is purely technical research, do not use in harmful scenes! Scheme

Now that Google has made specific technical demands, netizens have come up with a variety of workaround methods, such as using an empty Surfaceview or setting this preview to be transparent.
But I have failed both of these approaches. And the user Sam gave the preview set to a 1x1 size scheme played a role. The following demo shows this scenario:
The interface is very simple, a button to take pictures, the OnClick event method named Ontakephotoclicked.

 Public void ontakephotoclicked(View view) {FinalSurfaceview preview =NewSurfaceview ( This); Surfaceholder holder = Preview.getholder ();//deprecated setting, but required on Android versions prior to 3.0Holder.settype (surfaceholder.surface_type_push_buffers); Holder.addcallback (NewSurfaceholder.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 camera =NULL;Try{camera = Camera.open (Camera.CameraInfo.CAMERA_FACING_BACK); LOG.D (TAG,"opened Camera");Try{camera.setpreviewdisplay (holder); }Catch(IOException e) {Throw NewRuntimeException (e);                    } camera.startpreview (); LOG.D (TAG,"Started Preview"); Camera.takepicture (NULL,NULL, Picturecallback); }Catch(Exception e) {if(Camera! =NULL) Camera.release ();Throw NewRuntimeException (e); }            }@Override             Public void surfacedestroyed(Surfaceholder Holder) {}@Override             Public void surfacechanged(Surfaceholder holder,intFormatintWidthintHeight) {}});        WindowManager wm = (WindowManager) getsystemservice (Context.window_service); Windowmanager.layoutparams params =NewWindowmanager.layoutparams (1,1,//must bes at least 1x1WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,0,//don ' t know if this is a safe defaultPixelformat.unknown);//don ' t set the preview visibility to GONE or INVISIBLEWm.addview (preview, params); }
Image scaling and cropping

Now it's almost ready, Picturecallback is the callback after the successful photo shoot, and we'll do some work on the image data in this callback.
First, the image data is returned as a byte array, and the prototype is as follows:

        @Override        publicvoidonPictureTaken(byte[] data, Camera camera) {}

We're going to convert the array into bitmap, using the Bitmapfactory.decodebytearray method.
But you will find that the picture is inverted to the left, do not hurry, we have to put it right over. At this point we will use the Matrix.postrotate method with Bitmap.createbitmap to create a new bitmap.
Talk about scaling, refer to the Bitmap.createscaledbitmap method.
What about cutting it? Create a new Rect, and note that it's going to be clipped throughout the bitmap, such as in the program where I'm going to be 1/4 away from the original.
This is not over, I will save these three bitmap into three pictures after I have done the work. Note When doing bitmap compress operation, the second parameter quality is very important, the higher the quality of the picture, the greater the space occupied.
OK, here's the code, please refer to:

Private Camera. PicturecallbackPicturecallback = new Camera. Picturecallback() {@Override public void Onpicturetaken (byte[] data, camera camera) {Log. D(TAG,"Onpicturetaken");if (null = = data) {return;} Bitmap Bitmap = Bitmapfactory. Decodebytearray(Data,0, the data. Length);Camera. Stoppreview();Matrix matrix = new Matrix ();Matrix. Postrotate((float)90.0);Bitmap = Bitmap. CreateBitmap(Bitmap,0,0, bitmap. GetWidth(), Bitmap. GetHeight(), Matrix, False);Log. D(TAG,"Original bitmap width:"+ Bitmap. GetWidth() +"Height:"+ Bitmap. GetHeight());Bitmap Sizebitmap = Bitmap. Createscaledbitmap(Bitmap, bitmap. GetWidth()/3, bitmap. GetHeight()/3, true);Log. D(TAG,"size bitmap width"+sizebitmap. GetWidth()+"Height"+sizebitmap. GetHeight());Crop bitmap int leftoffset = (int) (Sizebitmap. GetWidth() *0.25);int topoffset = (int) (Sizebitmap. GetHeight() *0.25);Rect rect = new Rect (LeftOffset, Topoffset, Sizebitmap. GetWidth()-LeftOffset, Sizebitmap. GetHeight()-Topoffset);Bitmap Rectbitmap = Bitmap. CreateBitmap(Sizebitmap, rect. Left, rect. Top, rect. Width(), Rect. Height());try {fileoutputstream outputstream = new FileOutputStream (Environment. getExternalStorageDirectory(). toString()+"/photoresize.jpg");Sizebitmap. Compress(Bitmap. Compressformat. JPEG, -, OutputStream);OutputStream. Close();FileOutputStream outputstreamoriginal = new FileOutputStream (Environment. getExternalStorageDirectory(). toString()+"/photooriginal.jpg");Bitmap. Compress(Bitmap. Compressformat. JPEG, -, outputstreamoriginal);Outputstreamoriginal. Close();FileOutputStream outputstreamcut = new FileOutputStream (Environment. getExternalStorageDirectory(). toString()+"/photocut.jpg");Rectbitmap. Compress(Bitmap. Compressformat. JPEG, -, Outputstreamcut);Outputstreamcut. Close();Log. D(TAG,"Picture saved!");} catch (Exception e) {E. Printstacktrace();}        }    };
The permissions you need:
    <uses-permission android:name="Android.permission.SYSTEM_ALERT_WINDOW" />     <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE" / >    <uses-permission android:name="Android.permission.CAMERA" />    <uses-feature android:name="Android.hardware.camera" />    <uses-feature android:name="Android.hardware.camera.autofocus" />

Reference:
Http://stackoverflow.com/questions/2386025/taking-picture-from-camera-without-preview

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android 47: Do not preview photos and picture zoom clipping

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.