获取图像从相机到ImageView的机器人 [英] Get Image from camera into ImageView Android

查看:167
本文介绍了获取图像从相机到ImageView的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面code能很好地得到相机拍摄的照片的缩略图。我想获得完整的图像,而不是缩略图。我有一个三星Galaxy Nexus的

 私有静态最终诠释CAMERA_PIC_REQUEST = 1111;

私人ImageView的mImage;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_my_camera);
        mImage =(ImageView的)findViewById(R.id.imageView1);
        意向意图=新的意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
        startActivityForResult(意向,CAMERA_PIC_REQUEST);
    }

    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        如果(要求code == CAMERA_PIC_REQUEST){
            。位图的缩略图=(位图)data.getExtras()获得(数据);
            mImage.setImageBitmap(缩略图);
            ByteArrayOutputStream字节=新ByteArrayOutputStream();
            thumbnail.com preSS(Bitmap.Com pressFormat.JPEG,100个字节);
            档案文件=新的文件(Environment.getExternalStorageDirectory()+文件分割符+image.jpg文件);
            尝试 {
                file.createNewFile();
                FileOutputStream中FO =新的FileOutputStream(文件);
                fo.write(bytes.toByteArray());
                fo.close();
            }赶上(IOException异常E){
                // TODO自动生成的catch块
                e.printStackTrace();
            }
        }
    }
 

解决方案

要得到你需要先将其保存到文件,然后提取它是从它的位图一个全尺寸的画面,做到这一点:

 意向意图=新的意图(android.media.action.IMAGE_CAPTURE);
 档案文件=新的文件(Environment.getExternalStorageDirectory()+文件分割符+image.jpg文件);
 intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(文件));
 startActivityForResult(意向,CAPTURE_NEW_PICTURE);
 

然后在 onActivityResult

 文件文件=新的文件(Environment.getExternalStorageDirectory()+文件分割符+image.jpg文件);
 mImage =(ImageView的)findViewById(R.id.imageView1);
 mImage.setImageBitmap(德codeSampledBitmapFromFile(file.getAbsolutePath(),500,250));
 

德codeSampledBitmapFromFile

 公共静态位图德codeSampledBitmapFromFile(字符串路径,
        INT reqWidth,诠释reqHeight){//最好的质量MATCH

    //第一代code与inJustDe codeBounds = true来检查尺寸
    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    BitmapFactory.de codeFILE(路径选择);

    //计算inSampleSize
        //原始高度和宽度的图像
        最终诠释身高= options.outHeight;
        最终诠释宽度= options.outWidth;
        options.in preferredConfig = Bitmap.Config.RGB_565;
        INT inSampleSize = 1;

        如果(高度> reqHeight){
            inSampleSize = Math.round((浮动)的高度/(浮点)reqHeight);
        }

        INT expectedWidth =宽度/ inSampleSize;

        如果(expectedWidth> reqWidth){
            //if(Math.round((float)width /(浮点)reqWidth)> inSampleSize)//如果大SampSize ..
            inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    与inSampleSize集//德code位图
    options.inJustDe codeBounds = FALSE;

    返回BitmapFactory.de codeFILE(路径选择);
  }
 

您可以用数字(500,在这种情况下,250)起到改变位图的品质的ImageView

The following code works well to get the thumbnail of a picture taken from the camera. I want to get the full image, not just the thumbnail. I have a Samsung Galaxy Nexus

private static final int CAMERA_PIC_REQUEST = 1111;

private ImageView mImage;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_camera);
        mImage = (ImageView) findViewById(R.id.imageView1);
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_PIC_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            mImage.setImageBitmap(thumbnail);
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
            try {
                file.createNewFile();
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

解决方案

To get a full size picture you need to first save it to file and then extract it's bitmap from it, do this:

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
 startActivityForResult(intent, CAPTURE_NEW_PICTURE);

Then in onActivityResult:

 File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
 mImage = (ImageView) findViewById(R.id.imageView1);
 mImage.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250));

When decodeSampledBitmapFromFile:

    public static Bitmap decodeSampledBitmapFromFile(String path,
        int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        }

        int expectedWidth = width / inSampleSize;

        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }


    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
  }

You can play with the numbers (500 and 250 in this case) to change the quality of the bitmap for the ImageView.

这篇关于获取图像从相机到ImageView的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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