This article describes the usage of camera in Android. Share it for your reference. The details are as follows:
1. Regarding the preview of the horizontal and vertical difference of 90 degrees
Cause analysis
After verification and experimentation, it can be confirmed that the SDK () provided by Android may not be able to load the camera normally using portrait layout. When loading the camera in portrait mode, the following situations will occur:
①. Camera imaging 90 degrees left tilt (tilt);
②. The camera imaging length and width ratio is incorrect (miss ratio).
The reason why it is "probably" is that it may be solved through some more complex means. If the above is true, then it will be obvious why the vertical screen cannot image normally. Why this happens, please see the research and analysis below.
In general, the camera must use landscape layout (horizontal screen). It can be proved that you should write a camera first (as long as you can preview). If android:screenOrientation="landscape" is not added to the Manifest activity, that is, the default android:screenOrientation="portrait" will appear when the camera previews, and the left tilt will appear 90 degrees, and the ratio will be lost. The reason is this (I speculated) that the mapping of the camera control object is fixed on the bottom layer of Android, positive in the landscape method, and an image of size 320*480 is generated. If it is replaced with the portrait method, the camera will still generate an image of 320*480, and then it will be placed into a 480*320 screen, which will obviously lose the ratio. Then, according to the rules of vertical and horizontal screens, the left tilt is 90 degrees. In order to further confirm my speculation about the reason for the mismatch, the SurfaceView loaded in my camera was adjusted to 320*213, with a ratio of about (320:213)*1.5=(480:320). The imaging result was as expected, but there was no mismatch, which confirmed my idea.
From the above, we can see that the left tilt is generated by camera mapping, while the error ratio is generated by pixel proportion mapping.
Solution
There is no good solution yet, so you can only force horizontal screen and add it to the record code.
There is no good solution yet, so you can only force horizontal screen and add it to the record code.
2. The photos taken cannot be imaged correctly, such as green screen, red and green intersecting, overlapping, etc.
Cause analysis
Some mobile phones do not support (width,height) and (width,height) methods. It is recommended not to set these two methods for compatibility.
Attachment: Complete sample code:
Layout file:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:layout_weight="2"> <SurfaceView android: android:layout_width="fill_parent" android:layout_height="fill_parent" /> </LinearLayout> <LinearLayout xmlns:andro android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_weight="1" android:background="@android:color/white" android:layout_gravity="center"> <ImageButton android:layout_width="fill_parent" android:layout_height="80dip" android: android:layout_gravity="center" android:textSize="30dip" android:layout_weight="1" android:src="@drawable/btn_take_pic"/> <ImageButton android:layout_width="fill_parent" android:layout_height="80dip" android: android:layout_gravity="center" android:textSize="30dip" android:layout_weight="1" android:src="@drawable/btn_auto_focus"/> </LinearLayout> </LinearLayout>
2. MainActivity photo core code:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private ImageButton btnTakePicture = null; private ImageButton btnAutoFocus = null; private Camera camera = null; @Override public void onCreate(Bundle savedInstanceState) { (savedInstanceState); // Set window title requestWindowFeature(Window.FEATURE_NO_TITLE); // Horizontal screen setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); // Full screen display getWindow().setFlags(.FLAG_FULLSCREEN, .FLAG_FULLSCREEN); // When this window is visible to the user, keep the device open and keep the brightness unchanged. getWindow().addFlags(.FLAG_KEEP_SCREEN_ON); setContentView(); SurfaceView surfaceView = (SurfaceView) this .findViewById(); () .setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); ().setFixedSize(320, 240); // Set resolution ().addCallback(new SurfaceCallback()); btnTakePicture = (ImageButton) findViewById(); btnAutoFocus = (ImageButton) findViewById(); (onClickListener); (onClickListener); } private final onClickListener = new () { @Override public void onClick(View v) { if (v == btnTakePicture) { if (camera != null) (null, null, new TakePictureCallback()); // Photograph } else if (v == btnAutoFocus) { if (camera != null) (null); // Focus } } }; private final class SurfaceCallback implements Callback { private boolean preview; // Is it previewing? @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceCreated(SurfaceHolder holder) { try { camera = (); parameters = (); (5); //5 frames per second ();//Set the output format of the photo ("jpeg-quality", 85);//Photo quality (parameters); (holder); (); preview = true; } catch (Exception e) { (); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null) { if (preview) { (); preview = false; } (); camera = null; // Remember to release } } } private final class TakePictureCallback implements PictureCallback { public void onPictureTaken(byte[] data, Camera camera) { Bitmap bitmap = (data, 0, ); Matrix matrix=new Matrix(); //Set zoom (0.5f, 0.5f); bitmap=(bitmap, 0, 0, (), (), matrix, true); File file = new File((), () + ".jpg"); try { FileOutputStream outStream = new FileOutputStream(file); (, 100, outStream); (); (); } catch (Exception e) { (); } } } }
List file:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:andro package="" android:versionCode="1" android:versionName="1.0" > <application android:icon="@drawable/icon" android:label="@string/app_name" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="" /> <category android:name="" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="7" /> <uses-permission android:name="" /> <!-- existSDCardCreate and delete files permissions --> <uses-permission android:name=".MOUNT_UNMOUNT_FILESYSTEMS" /> <!-- PastSDCardWrite data permissions --> <uses-permission android:name=".WRITE_EXTERNAL_STORAGE" /> </manifest>
I hope this article will be helpful to everyone's Android programming design.