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
-
Go Launcher Minimal MIUI Theme v1.9 APKThis theme contains 220+ icons & counting! & it also contains the 3 wallpapers seen in the …
-
War Pinball HD v1.1 (Paid Version) Android Apk Game DownloadWar Pinball HD v1.1 Requirements: Android 2.3.3 and up Overview: The amazing new War Pinball HD…
-
Bombergeddon Premium (Qvga, Hvga)Di Game ini seorang tokoh petualang dunia yang terkenal di film layar lebar. akan meramaikan game A…
-
War Pinball HD v1.1 (Paid Version) Android Apk Game DownloadWar Pinball HD v1.1 Requirements: Android 2.3.3 and up Overview: The amazing new War Pinball HD…
-
Rule The Kingdom v3.01Rule the Kingdom: fantastic RPG action, city-building, farming and storytelling! *NEW!* EPIC UPDATE…
-
Asphalt 7: Heat HD v1.0.1 [Gameloft store] (Paid Version) Android Apk Game DownloadAsphalt 7: Heat HD v1.0.1 [Gameloft store] Requirements: Android 2.2 and up Overview: Hit the s…
-
GT Racing: Motor AcademyJoin the Academy! It won't cost a thing to get started! Experience the richest racing simulatio…

Post a Comment
Post a Comment