Android Open Source - introToDroid4ed Still Image Activity






From Project

Back to project page introToDroid4ed.

License

The source code is released under:

GNU General Public License

If you think the Android project introToDroid4ed listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.introtoandroid.simplemultimedia; 
// w  w  w  .  j  a  va 2  s  .c o  m
import java.io.FileOutputStream;
import java.util.List;
import java.util.ListIterator;


import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

public class StillImageActivity extends Activity {
    private static final String DEBUG_TAG = "StillImageActivity";
    final private static String STILL_IMAGE_FILE = "capture.jpg";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.still);
        final CameraSurfaceView cameraView = new CameraSurfaceView(
                getApplicationContext());
        FrameLayout frame = (FrameLayout) findViewById(R.id.frame);
        frame.addView(cameraView);
        Button capture = (Button) findViewById(R.id.capture);
        capture.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.v(DEBUG_TAG, "Requesting capture");
                cameraView.capture(new Camera.PictureCallback() {
                    public void onPictureTaken(byte[] data, Camera camera) {
                        Log.v("Still", "Image data received from camera");
                        FileOutputStream fos;
                        try {
                            // Fully qualified path name. In this case, we
                            // use the Files subdir
                            String pathForAppFiles = getFilesDir()
                                    .getAbsolutePath();
                            pathForAppFiles = pathForAppFiles + "/"
                                    + STILL_IMAGE_FILE;
                            Log.d("Still image filename:", pathForAppFiles);
                            fos = openFileOutput(STILL_IMAGE_FILE, 0);
                            fos.write(data);
                            fos.close();
                        } catch (Exception e) {
                            Log.e("Still", "Error writing file", e);
                        }
                    }
                });
            }
        });
        Button paper = (Button) findViewById(R.id.wallpaper);
        paper.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                cameraView.capture(new Camera.PictureCallback() {
                    public void onPictureTaken(byte[] data, Camera camera) {
                        Log.v("Still", "Image data received from camera");
                        Bitmap recordedImage = BitmapFactory.decodeByteArray(
                                data, 0, data.length);
                        try {
                            WallpaperManager wpManager = WallpaperManager
                                    .getInstance(StillImageActivity.this);
                            int height = wpManager.getDesiredMinimumHeight();
                            int width = wpManager.getDesiredMinimumWidth();
                            Toast.makeText(getApplicationContext(),
                                    "Wallpaper size = " + width + "x" + height,
                                    Toast.LENGTH_LONG).show();
                            Log.v("Still", "Wallpaper size=" + width + "x"
                                    + height);
                            Bitmap scaledImage = Bitmap.createScaledBitmap(
                                    recordedImage, width, height, false);
                            wpManager.setBitmap(scaledImage);
                        } catch (Exception e) {
                            Log.e("Still", "Setting wallpaper failed.", e);
                        }
                    }
                });
            }
        });
    }

    private class CameraSurfaceView extends SurfaceView implements
            SurfaceHolder.Callback {
        private Camera camera = null;
        private SurfaceHolder mHolder = null;
        private int cameraId = 0;

        public CameraSurfaceView(Context context) {
            super(context);
            // Install a SurfaceHolder.Callback so we get notified when the
            // underlying surface is created and destroyed
            mHolder = getHolder();
            mHolder.addCallback(this);

            // optional -- use the front facing camera
            CameraInfo info = new CameraInfo();
            for (int camIndex = 0; camIndex < Camera.getNumberOfCameras(); camIndex++) {
                Camera.getCameraInfo(camIndex, info);
                if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                    cameraId = camIndex;
                    break;
                }
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            try {
                Camera.Parameters params = camera.getParameters();
                // not all cameras supporting setting arbitrary sizes
                List<Camera.Size> sizes = params.getSupportedPreviewSizes();
                Camera.Size pickedSize = getBestFit(sizes, width, height);
                if (pickedSize != null) {
                    params.setPreviewSize(pickedSize.width, pickedSize.height);
                    Log.d(DEBUG_TAG, "Preview size: (" + pickedSize.width + ","
                            + pickedSize.height + ")");
                    // even after setting a supported size, the preview size may
                    // still end up just being the surface size (supported or
                    // not)
                    camera.setParameters(params);
                }
                // set the orientation to standard portrait.
                // Do this only if you know the specific orientation (0,90,180,
                // etc.)
                // Only works on API Level 8+
                if  (pickedSize != null) {
                    setCameraDisplayOrientation(pickedSize.width, pickedSize.height);
                } else {
                    Camera.Size previewSize = camera.getParameters().getPreviewSize();
                    setCameraDisplayOrientation(previewSize.width, previewSize.height);
                }
                camera.startPreview();
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to set preview size", e);
            }
        }

        private Size getBestFit(List<Size> sizes, int width, int height) {
            Size bestFit = null;
            ListIterator<Size> items = sizes.listIterator();
            while (items.hasNext()) {
                Size item = items.next();
                if (item.width <= width && item.height <= height) {
                    if (bestFit != null) {
                        // if our current best fit has a smaller area, then we
                        // want the new one (bigger area == better fit)
                        if (bestFit.width * bestFit.height < item.width
                                * item.height) {
                            bestFit = item;
                        }
                    } else {
                        bestFit = item;
                    }
                }
            }
            return bestFit;
        }

        private void setCameraDisplayOrientation(int width, int height) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            int rotation = getWindowManager().getDefaultDisplay().getRotation();
            int degrees = rotation * 90;

            int result;
            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;
            } else {
                result = (info.orientation - degrees + 360) % 360;
            }
            camera.setDisplayOrientation(result);

            if (result == 90 || result == 270) {
                mHolder.setFixedSize(height, width);
            } else {
                mHolder.setFixedSize(width, height);

            }
        }

        public void surfaceCreated(SurfaceHolder holder) {
            camera = Camera.open(cameraId);
            try {
                camera.setPreviewDisplay(mHolder);
            } catch (Exception e) {
                Log.e(DEBUG_TAG, "Failed to set camera preview display", e);
            }
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
            camera.stopPreview();
            camera.release();
            camera = null;
        }

        public boolean capture(Camera.PictureCallback jpegHandler) {
            if (camera != null) {
                camera.takePicture(null, null, jpegHandler);
                return true;
            } else {
                return false;
            }
        }
    }
}




Java Source Code List

com.introtoandroid.advancedlayouts.AdaptersActivity.java
com.introtoandroid.advancedlayouts.AdvancedLayoutsActivity.java
com.introtoandroid.advancedlayouts.BasicLayoutActivity.java
com.introtoandroid.advancedlayouts.ContactAdapterActivity.java
com.introtoandroid.advancedlayouts.DialogActivity.java
com.introtoandroid.advancedlayouts.GridAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.GridLayoutActivity.java
com.introtoandroid.advancedlayouts.GridListMenuActivity.java
com.introtoandroid.advancedlayouts.ListAdapterSampleActivity.java
com.introtoandroid.advancedlayouts.MenuActivity.java
com.introtoandroid.advancedlayouts.MyListActivity.java
com.introtoandroid.advancedlayouts.StyleSamplesActivity.java
com.introtoandroid.filesoc.FileStreamOfConsciousnessActivity.java
com.introtoandroid.filesoc.ViewLogActivity.java
com.introtoandroid.myfirstandroidapp.MyFirstAndroidAppActivity.java
com.introtoandroid.navigation.FirstChildActivity.java
com.introtoandroid.navigation.ParentActivity.java
com.introtoandroid.navigation.SecondChildActivity.java
com.introtoandroid.navigation.ThirdChildActivity.java
com.introtoandroid.parisview.ParisViewActivity.java
com.introtoandroid.passwordmatcher.PasswordMatcherActivity.java
com.introtoandroid.resourceroundup.ResourceRoundupActivity.java
com.introtoandroid.samelayout.MenuActivity.java
com.introtoandroid.samelayout.ProgrammaticLayoutActivity.java
com.introtoandroid.samelayout.ResourceLayoutActivity.java
com.introtoandroid.samelayout.SameLayoutActivity.java
com.introtoandroid.simpleactionbar.SimpleActionBarActivity.java
com.introtoandroid.simplealtresources.SimpleAltResourcesActivity.java
com.introtoandroid.simplecontacts.SimpleContactsActivity.java
com.introtoandroid.simplecontentprovider.MenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleBookmarks.java
com.introtoandroid.simplecontentprovider.SimpleCallLog.java
com.introtoandroid.simplecontentprovider.SimpleContentProviderMenuActivity.java
com.introtoandroid.simplecontentprovider.SimpleMediaStore.java
com.introtoandroid.simplefiles.SimpleFilesActivity.java
com.introtoandroid.simplefragdialogs.SimpleFragDialogActivity.java
com.introtoandroid.simplefragments.FieldNoteListFragment.java
com.introtoandroid.simplefragments.FieldNoteViewActivity.java
com.introtoandroid.simplefragments.FieldNoteWebViewFragment.java
com.introtoandroid.simplefragments.SimpleFragmentsActivity.java
com.introtoandroid.simplelayout.FrameLayoutActivity.java
com.introtoandroid.simplelayout.GridLayoutActivity.java
com.introtoandroid.simplelayout.LinearLayoutActivity.java
com.introtoandroid.simplelayout.MenuActivity.java
com.introtoandroid.simplelayout.MultipleLayoutActivity.java
com.introtoandroid.simplelayout.RelativeLayoutActivity.java
com.introtoandroid.simplelayout.SimpleLayoutActivity.java
com.introtoandroid.simplelayout.TableLayoutActivity.java
com.introtoandroid.simplemultimedia.AudioActivity.java
com.introtoandroid.simplemultimedia.MenuActivity.java
com.introtoandroid.simplemultimedia.SimpleMultimediaActivity.java
com.introtoandroid.simplemultimedia.StillImageActivity.java
com.introtoandroid.simplemultimedia.VideoPlayActivity.java
com.introtoandroid.simplepreferences.MoreSimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SimplePreferencesActivity.java
com.introtoandroid.simplepreferences.SuperSimplePreferencesActivity.java
com.introtoandroid.simpleresourceview.SimpleResourceViewActivity.java
com.introtoandroid.simplescrolling.BothScrollActivity.java
com.introtoandroid.simplescrolling.HorizontalScrollActivity.java
com.introtoandroid.simplescrolling.MenuActivity.java
com.introtoandroid.simplescrolling.NoScrollActivity.java
com.introtoandroid.simplescrolling.SimpleScrollingActivity.java
com.introtoandroid.simplescrolling.VerticalScrollActivity.java
com.introtoandroid.simpleuserprefs.SimpleUserPrefsActivity.java
com.introtoandroid.supportfragdialog.MyAlertDialogFragment.java
com.introtoandroid.supportfragdialog.SupportFragDialogActivity.java
com.introtoandroid.userprefsheaders.UserPrefsActivity.java
com.introtoandroid.viewsamples.ButtonsActivity.java
com.introtoandroid.viewsamples.ContainersActivity.java
com.introtoandroid.viewsamples.EventsActivity.java
com.introtoandroid.viewsamples.FormsActivity.java
com.introtoandroid.viewsamples.IndicatorsActivity.java
com.introtoandroid.viewsamples.MenuActivity.java
com.introtoandroid.viewsamples.PickersActivity.java
com.introtoandroid.viewsamples.TextDisplayActivity.java
com.introtoandroid.viewsamples.TextInputActivity.java
com.introtoandroid.viewsamples.ViewSampleActivity.java