Home
› Uncategorized
It'a example to determine the best supported camera preview size, to have the biggest preview area.
Modify the layout to have a SurfaceView for preview.
Modify AndroidManifest.xml to add permission of "android.permission.CAMERA", and set android:screenOrientation="landscape".
Download the files.
package com.example.androidcamera; import java.io.IOException; import java.util.List; import android.hardware.Camera; import android.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.Toast; import android.app.Activity; public class MainActivity extends Activity { Camera myCamera; SurfaceView mySurfaceView; SurfaceHolder mySurfaceHolder; boolean isPreview; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); isPreview = false; mySurfaceView = (SurfaceView)findViewById(R.id.mypreview); mySurfaceHolder = mySurfaceView.getHolder(); mySurfaceHolder.addCallback(mySurfaceCallback); } SurfaceHolder.Callback mySurfaceCallback = new SurfaceHolder.Callback(){ @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { Camera.Parameters myParameters = myCamera.getParameters(); Camera.Size myBestSize = getBestPreviewSize(width, height, myParameters); if(myBestSize != null){ myParameters.setPreviewSize(myBestSize.width, myBestSize.height); myCamera.setParameters(myParameters); myCamera.startPreview(); isPreview = true; Toast.makeText(getApplicationContext(), "Best Size:\n" + String.valueOf(myBestSize.width) + " : " + String.valueOf(myBestSize.height), Toast.LENGTH_LONG).show(); } } @Override public void surfaceCreated(SurfaceHolder holder) { try { myCamera.setPreviewDisplay(mySurfaceHolder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void surfaceDestroyed(SurfaceHolder holder) { // TODO Auto-generated method stub } }; private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters){ Camera.Size bestSize = null; List<Camera.Size> sizeList = parameters.getSupportedPreviewSizes(); bestSize = sizeList.get(0); for(int i = 1; i < sizeList.size(); i++){ if((sizeList.get(i).width * sizeList.get(i).height) > (bestSize.width * bestSize.height)){ bestSize = sizeList.get(i); } } return bestSize; } @Override protected void onResume() { super.onResume(); myCamera = Camera.open(); } @Override protected void onPause() { if(isPreview){ myCamera.stopPreview(); } myCamera.release(); myCamera = null; isPreview = false; super.onPause(); } }
Modify the layout to have a SurfaceView for preview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <SurfaceView android:id="@+id/mypreview" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
Modify AndroidManifest.xml to add permission of "android.permission.CAMERA", and set android:screenOrientation="landscape".
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidcamera" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <uses-permission android:name="android.permission.CAMERA"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

-- Delivered by Feed43 service
Related Posts
There is no other posts in this category.Popular
-
Real football 2010 HD 480x800 (update)Lead your team to the championship and make trades to recruit the greatest players, or change the…
-
PicoBrothers YouTube Downloader v2.2.2 BetaPicoBrothers YouTube Downloader v2.2.2 Beta Support/Info: S60v5/S^3 Signed Details: YouTube Downloa…
-
Angry Birds SpaceAPK How to install? 1. Install directly to your phone 2. Enjoy
-
Text Bomber for AndroidText Bomber for Android In a previous post I've ever shared about SMS Bomber Application for An…
-
[Android] Need for Speed: Hot Pursuit v1.0.18In Need for Speed: Hot Pursuit the cops are back and are looking to make up for their lost time. U…
-
Ripple Lock v1.6Ripple Lock v1.6 Requirements: for Android version 2.1 and higher Overview: Is still envy of the br…
-
[Android] Paladog 2 Free Full Download apkPaladog 2 , an excellent strategy/simulation game, has released on Android Market since 2011. The…
Post a Comment
Post a Comment