Android capture and display image app

≡放荡痞女 提交于 2019-12-11 19:45:46

问题


I am kind of new to android app development and trying to make a simple application which,

  1. Uses the system camera application to take picture by passing camera intent.
  2. Stores the image in the public folder in file system.
  3. Then takes the image from its absolute path convert it into bitmap and display it in image view by performing some scale down on it.

Here is my code....

static final int REQUEST_IMAGE_CAPTURE = 1;
String mCurrentPhotoPath;
Button captureImageButton;
static ImageView imageCapturedView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_image);




    captureImageButton = (Button) findViewById(R.id.button);

    OnClickListener captureImageListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent captureImageIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            if(captureImageIntent.resolveActivity(getPackageManager())!=null)
            {
                File photoFile=null;

                try{
                    photoFile = createImageFile();

                }catch(IOException e){};

                if(photoFile != null)
                {

                    captureImageIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                    startActivityForResult(captureImageIntent, REQUEST_IMAGE_CAPTURE);
                }

            }

        }
    };

    captureImageButton.setOnClickListener(captureImageListener);

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    imageCapturedView = (ImageView)findViewById(R.id.ImageView);

    if(requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK)
    {
        Bundle extras = data.getExtras();
        Bitmap imageBitmap= (Bitmap) extras.get("data");

        imageCapturedView.setImageBitmap(imageBitmap);
        galleryAddPic();
        setPic();
    }
}

private File createImageFile() throws IOException
{

    String TimeStamp = new SimpleDateFormat("yyyyMMDdd_HHmmss").format(new Date());
    String ImageFile = "JPEG_" + TimeStamp + "_";
    File StorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

    File image = File.createTempFile(ImageFile, ".jpg", StorageDir); 

    mCurrentPhotoPath = image.getAbsolutePath();

    return image;

}

private void galleryAddPic()
{
    Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    File f= new File(mCurrentPhotoPath);
    Uri contentUri = Uri.fromFile(f);
    mediaScan.setData(contentUri);
    this.sendBroadcast(mediaScan);

}


private void setPic() throws ArithmeticException{


    int scaleFactor;
    Bitmap bitmap;
    // Get the dimensions of the View
    int targetW = imageCapturedView.getWidth();
    int targetH = imageCapturedView.getHeight();


    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    //bmOptions.inSampleSize = 4;
    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
        scaleFactor= Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;



    bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);

    Toast.makeText(getApplicationContext(), mCurrentPhotoPath, Toast.LENGTH_LONG).show();
    imageCapturedView.setImageBitmap(bitmap);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.take_image, menu);
    return true;
}

}

Here the problem lies with the setPic function according to me but don't know what it is as i have just followed the tutorial given on developers.android

I am able to capture the image and store it in the sd card but not able to display it in the image view. Something going wrong during scaling the bitmap according to the image view properties.

This is the error that i am getting

01-20 14:32:34.736: E/AndroidRuntime(2513): FATAL EXCEPTION: main
01-20 14:32:34.736: E/AndroidRuntime(2513): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data dat=file:///storage/sdcard0/Pictures/JPEG_2014012020_143223_-1472164486.jpg typ=image/jpeg (has extras) }} to activity {yogesh.atArxxus.ocr_trial_application/yogesh.atArxxus.ocr_trial_application.Take_image}: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3161)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3204)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.access$1100(ActivityThread.java:137)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1254)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.os.Looper.loop(Looper.java:213)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.main(ActivityThread.java:4793)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invokeNative(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at java.lang.reflect.Method.invoke(Method.java:511)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at dalvik.system.NativeStart.main(Native Method)
01-20 14:32:34.736: E/AndroidRuntime(2513): Caused by: java.lang.ArithmeticException: divide by zero
01-20 14:32:34.736: E/AndroidRuntime(2513):     at yogesh.atArxxus.ocr_trial_application.Take_image.setPic(Take_image.java:136)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at yogesh.atArxxus.ocr_trial_application.Take_image.onActivityResult(Take_image.java:87)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.Activity.dispatchActivityResult(Activity.java:5192)
01-20 14:32:34.736: E/AndroidRuntime(2513):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3157)

Thank you


回答1:


In the line "scaleFactor= Math.min(photoW/targetW, photoH/targetH);", either targetW or targetH or both are zero. Just include a simple validation like this, right before that line.

if(targetH == 0) targetH = 1;
if(targetW == 0) targetW = 1;

Your next step should be in debugging why one of those contain zero.




回答2:


Try this but its not tested properly

public class MainActivity extends Activity {

    // Activity request codes
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    public static int RESULT_LOAD_IMAGE = 1; 

    // directory name to store captured images and videos
    private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";

    private Uri fileUri; // file url to store image/video

    private ImageView imgPreview;
    private VideoView videoPreview;
    private Button btnCapturePicture, btnRecordVideo;
    private ImageButton captureImage,browsImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgPreview = (ImageView) findViewById(R.id.imgPreview);
        videoPreview = (VideoView) findViewById(R.id.videoPreview);
        btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
        btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);
        captureImage = (ImageButton) findViewById(R.id.captureImage);
        browsImage = (ImageButton) findViewById(R.id.browsImage);


        /*
         * Capture image button click event
         */
        captureImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // capture picture
                captureImage();
            }
        });


        browsImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // capture pictur
                Intent i = new Intent(Intent.ACTION_PICK,  
                android.provider.MediaStore  
                              .Images.Media.EXTERNAL_CONTENT_URI);  
                startActivityForResult(i, RESULT_LOAD_IMAGE); 
                }
        });
        /*
         * Record video button click event
         */
        btnRecordVideo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // record video
                recordVideo();
            }
        });

        // Checking camera availability
        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }
    }

    /**
     * Checking device has camera hardware or not
     * */
    private boolean isDeviceSupportCamera() {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    /*
     * Capturing Camera Image will lauch camera app requrest image capture
     */
    private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    /*
     * Here we store the file url as it will be null after returning from camera
     * app
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on scren orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }

    /*
     * Recording video
     */
    private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        // set video quality
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
                                                            // name

        // start the video capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    }

    /**
     * Receiving activity result method will be called after closing the camera
     * */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // if the result is capturing Image

        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // successfully captured the image
                // display it in image view
                previewCapturedImage();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        } else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // video successfully recorded
                // preview the recorded video
                previewVideo();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled recording
                Toast.makeText(getApplicationContext(),
                        "User cancelled video recording", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to record video
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to record video", Toast.LENGTH_SHORT)
                        .show();
            }
        }
        else if (requestCode == RESULT_LOAD_IMAGE) {

            super.onActivityResult(requestCode, resultCode, data);  

            if (resultCode == RESULT_OK && null != data) {  
                Uri selectedImage = data.getData();  
                String[] filePathColumn = { MediaStore.Images.Media.DATA };  

                Cursor cursor = getContentResolver().query(selectedImage,  
                        filePathColumn, null, null, null);  
                cursor.moveToFirst();  

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
                String picturePath = cursor.getString(columnIndex);  
                cursor.close();  
                Bitmap bm=BitmapFactory  
                        .decodeFile(picturePath);
                Bitmap pic=ShrinkBitmap(picturePath,100,100);

                imgPreview = (ImageView) findViewById(R.id.imgPreview);  
                imgPreview.setVisibility(View.VISIBLE);
                imgPreview.setImageBitmap(pic); 


            }  
        }
    }

    /*
     * Display image from a path to ImageView
     */
    private void previewCapturedImage() {
        try {
            // hide video preview
            videoPreview.setVisibility(View.GONE);

            imgPreview.setVisibility(View.VISIBLE);

            // bimatp factory
            BitmapFactory.Options options = new BitmapFactory.Options();

            // downsizing image as it throws OutOfMemory Exception for larger
            // images
            options.inSampleSize = 6;



            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
                    options);

            try {
            File file = new File (fileUri.getPath());
            FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
            imgPreview.setImageBitmap(bitmap);
             out.flush();
               out.close();
            } catch (Exception e) {
                   e.printStackTrace();
            }
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }

    /*
     * Previewing recorded video
     */
    private void previewVideo() {
        try {
            // hide image preview
            imgPreview.setVisibility(View.GONE);

            videoPreview.setVisibility(View.VISIBLE);
            videoPreview.setVideoPath(fileUri.getPath());
            // start playing
            videoPreview.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * ------------ Helper Methods ---------------------- 
     * */

    /*
     * Creating file uri to store image/video
     */
    public Uri getOutputMediaFileUri(int type) {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
     * returning image / video
     */
    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else if (type == MEDIA_TYPE_VIDEO) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "VID_" + timeStamp + ".mp4");
        } else {
            return null;
        }

        return mediaFile;
    }

    Bitmap ShrinkBitmap(String file, int width, int height){

         BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
         return bitmap;
        }

}

**XML FILE

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:baselineAligned="false"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- Capture picture button -->

    <ImageButton
        android:id="@+id/captureImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/camera" />

    <ImageButton
        android:id="@+id/browsImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/brows" />

    <Button
        android:id="@+id/btnCapturePicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Take a Picture"
        android:visibility="gone"
        android:layout_marginBottom="10dp"/>

    <!-- Record video button -->
    <Button
        android:id="@+id/btnRecordVideo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:text="Record a Video" />
</LinearLayout>

<LinearLayout
    android:layout_width="0dp"
    android:layout_height="fill_parent"
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Preview" 
        android:padding="10dp"
        android:textSize="15dp"/>

    <!-- To display picture taken -->
    <ImageView
        android:id="@+id/imgPreview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone" />

    <!-- To preview video recorded -->
    <VideoView
        android:id="@+id/videoPreview"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:visibility="gone" />
</LinearLayout>



来源:https://stackoverflow.com/questions/21229917/android-capture-and-display-image-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!