Android 捕获和显示图像应用程序

Posted

技术标签:

【中文标题】Android 捕获和显示图像应用程序【英文标题】:Android capture and display image app 【发布时间】:2014-01-20 09:06:38 【问题描述】:

我是 android 应用开发的新手,正在尝试制作一个简单的应用程序,

    使用系统相机应用程序通过传递相机意图来拍照。 将图像存储在文件系统的公用文件夹中。 然后从其绝对路径中获取图像,将其转换为位图,并通过对其执行一些缩小操作将其显示在图像视图中。

这是我的代码....

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;

根据我的说法,这里的问题在于 setPic 函数,但我不知道它是什么,因为我刚刚按照 developer.android 上给出的教程进行操作

我能够捕获图像并将其存储在 sd 卡中,但无法在图像视图中显示它。根据图像视图属性缩放位图时出现问题。

这是我遇到的错误

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 ResultInfowho=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)

谢谢

【问题讨论】:

如果我删除缩放代码并简单地尝试在图像视图中显示图像,那么它就可以工作了。不知道有什么问题,我如何让 targetW targetH 这个值在某处为零。 我也有类似的问题。所以请检查下面的链接,它可能对你有用。 ***.com/questions/21136311/… @Biplab 那么你是否使用了那个图像选择器库...? 是的,我已经使用了它并且它的工作干净 @Biplab 感谢库工作正常,但实际上我上面的代码也完成了这件事......唯一的问题是通过缩小位图在图像视图中显示完整图像......在您提到的库中,它还提供缩略图而不是图像的预览。是这样吗..? 【参考方案1】:

在“scaleFactor= Math.min(photoW/targetW, photoH/targetH);”行中,targetW 或 targetH 或两者都为零。只需在该行之前添加一个像这样的简单验证即可。

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

您的下一步应该是调试其中一个包含零的原因。

【讨论】:

是的,这实际上是我的问题...我调试了程序并知道其中一个值变为零,因此用于缩放的公式的分母为 0 并且应用程序失败....但不是确定为什么这些值变成 0 ..?实际上没有理由..?我只是得到我的 imageView 的高度和宽度.. 在 xml 中,ImageView (R.id.ImageView) 的 layout-width 和 layout-height 尺寸是否固定? 知道了。请参考这些答案 - ***.com/questions/4680499/… 和 ***.com/questions/10411975/… 。实际上,更好的方法是将尺寸存储在尺寸值 xml 中,并使用该尺寸在 xml 布局文件中设置图像视图的尺寸,并使用相同的尺寸(来自值 xml)在 setPic() 方法中缩放图像 @Srikanh ys 明白了....错误是在 XML 中,我将图像视图的宽度和高度保持为 fillparent 和 wrapcontent 所以我得到的两个值都是 0...如果我保持固定dp 的高度和宽度值然后它可以工作.....甚至可以尝试使用 getMeasuredHeight 和 getMeasuredWIdth .....谢谢【参考方案2】:

试试这个,但它没有正确测试

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 文件

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

<LinearLayout
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:gravity="center"
    android:orientation="vertical" >

    <!-- Capture picture button -->

    <ImageButton
        android:id="@+id/captureImage"
        android:layout_
        android:layout_
        android:src="@drawable/camera" />

    <ImageButton
        android:id="@+id/browsImage"
        android:layout_
        android:layout_
        android:src="@drawable/brows" />

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

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

<LinearLayout
    android:layout_
    android:layout_
    android:layout_weight="1"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_
        android:layout_
        android:text="Preview" 
        android:padding="10dp"
        android:textSize="15dp"/>

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

    <!-- To preview video recorded -->
    <VideoView
        android:id="@+id/videoPreview"
        android:layout_
        android:layout_
        android:visibility="gone" />
</LinearLayout>

【讨论】:

以上是关于Android 捕获和显示图像应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何从 android 的视频视图中显示的 ip 摄像头的流式视频中捕获图像

即使从Android中的Gallery中删除后,是否可以将捕获的图像保留在应用程序存储中?

如何在android中获取捕获图像的路径

Android 使用内置的Camera应用程序捕获图像

保存图像后由于方向更改导致 Android 显示图像捕获错误

Android - 如何从相机捕获图像或从库中添加