Android的捕捉和显示图像的应用程序 [英] Android capture and display image app

查看:116
本文介绍了Android的捕捉和显示图像的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一种新的Andr​​oid应用程序开发,并努力使一个简单的应用程序,它,

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


  1. 使用系统相机应用程序通过将摄像机的意图进行拍照。

  2. 存储在公共文件夹中的图像文件系统。

  3. 然后从它的绝对路径采用图片它通过在下来执行一些规模转换为位图,并显示它图像视图。

下面是我的code ....

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;
}

}

下面的问题在于,根据我setPic功能,但不知道它是什么,因为我刚才在后面给出developers.android教程

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

我能够捕捉图像,并将其存储在SD卡,但不能在图像视图中显示它。什么根据图像视图属性缩放位图时走错了。

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.

这是我得到的错误

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)

感谢您

推荐答案

在该行比例因子= Math.min(photoW / targetW,photoH / targetH);,无论是targetW或targetH或两者均为零。只包含这样一个简单的验证,该行之前。

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.

这篇关于Android的捕捉和显示图像的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆