• 获取拍照图片,显示大图像


    获取拍照图片

    标准代码:

    关键是传地址

    显示大图像
    加载并显示一幅图像对内存使用情况具有显著的影响。例如, HTC G1 电话带有一个
    320 万像素的摄像头。 320 万像素的摄像头通常会捕获 2048×1536 像素的图像。显示如此
    大小的 32 位图像将需要超过 100 663kb 或大约 13MB 的内存。虽然我们的应用程序不一定
    会因此而耗尽内存,但是这肯定会使得内存更加容易耗尽。
    Android 提供了一个名为 BitmapFactory 的实用程序类,该程序类提供了一系列的静态
    方法,允许通过各种来源加载 Bitmap 图像。针对我们的需求,将从文件加载图像,并在最
    初的活动中显示它。幸运的是, BitmapFactory 中的可用方法将会调用 BitmapFactory.Options
    类,这使得我们能够定义如何将 Bitmap 读入内存。具体而言,当加载图像时,可以设置
    BitmapFactory 应该使用的采样大小。在 BitmapFactory.Options 中指定 inSampleSize 参数,
    这将表明一旦加载时结果 Bitmap 图像所占的比例。例如,在这里将 inSampleSize 设置为 8,
    这会产生一幅大小是原始图像大小 1/8 的图像。
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inSampleSize = 8;
    Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    imv.setImageBitmap(bmp);
    这是一种快速加载大图像的方法,但是没有真正考虑图像的原始大小,也没有考虑屏
    幕的大小。最好能够将图像缩放到刚好适合屏幕。
    下面的代码片段演示了如何使用显示维度来确定在加载图像时应该发生的减采样量。
    当使用这些方法时,应确保该图像尽可能多地填充显示范围。但如果该图像只是要在任何
    一个维度中显示 100 个像素,那么应该使用这个值而不是显示维度,可以通过如下方式获
    得该值。
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    int dw = currentDisplay.getWidth();
    int dh = currentDisplay.getHeight();
    为了确定图像的所有尺寸(用于计算),我们使用了 BitmapFactory 和 BitmapFactory. Options,
    Be From--http://bmbook.5d6d.com/第 1 章 Android 图像概述
    7
    并将 BitmapFactory.Options.inJustDecodeBounds 变量设置为 true。这将通知 BitmapFactory
    类只须返回该图像的范围,而无须尝试解码图像本身。当使用此方法时, BitmapFactory.
    Options.outHeight 和 BitmapFactory.Options.outWidth 变量将会被赋值。
    // 加载图像的尺寸而不是图像本身
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)dh);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)dw);
    Log.v("HEIGHTRATIO",""+heightRatio);
    Log.v("WIDTHRATIO",""+widthRatio);
    简单地将图像的尺寸除以显示的尺寸将获得显示的比率。然后,可以选择是否使用高
    度比率或宽度比率,这取决于它们当中谁更大。只须将这个比率作为 BitmapFactory.Options.
    inSampleSize 变量,这将产生一幅应该加载到内存中的图像,其尺寸接近于我们在这种情
    况下所需要的尺寸,也接近于显示本身的尺寸。
    // 如果两个比率都大于 1,
    // 那么图像的一条边将大于屏幕
    if (heightRatio > 1 && widthRatio > 1)
    {
    if (heightRatio > widthRatio)
    {
    // 若高度比率更大,则根据它缩放
    bmpFactoryOptions.inSampleSize = heightRatio;
    }
    else
    {
    // 若宽度比率更大,则根据它缩放
    bmpFactoryOptions.inSampleSize = widthRatio;
    }
    }
    //对它进行真正的解码
    bmpFactoryOptions.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeFile(imageFilePath, bmpFactoryOptions);
    下面是通过一个意图使用内置摄像头并显示结果图片的完整示例代码。图 1-3 显示了
    一幅由此示例生成的屏幕大小的结果图像。
    
    
    package com.apress.proandroidmedia.ch1.sizedcameraintent;
    import java.io.File;
    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.util.Log;
    import android.view.Display;
    import android.widget.ImageView;
    public class SizedCameraIntent extends Activity {
    final static int CAMERA_RESULT = 0;
    ImageView imv;
    String imageFilePath;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    imageFilePath = Environment.getExternalStorageDirectory()
    .getAbsolutePath() + "/myfavoritepicture.jpg";
    File imageFile = new File(imageFilePath);
    Uri imageFileUri = Uri.fromFile(imageFile);
    Intent i = new Intent(android.provider.MediaStore.
    ACTION_IMAGE_CAPTURE);
    i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageFileUri);
    startActivityForResult(i, CAMERA_RESULT);
    }
    protected void onActivityResult(int requestCode, int resultCode,
    Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (resultCode == RESULT_OK)
    {
    // 获取 ImageView 的引用
    imv = (ImageView) findViewById(R.id.ReturnedImageView);
    Display currentDisplay = getWindowManager().getDefaultDisplay();
    int dw = currentDisplay.getWidth();
    int dh = currentDisplay.getHeight();
    // 加载图像的尺寸而不是图像本身
    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory
    .Options();
    bmpFactoryOptions.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(imageFilePath,
    bmpFactoryOptions);
    int heightRatio = (int)Math.ceil(bmpFactoryOptions.
    outHeight/(float)dh);
    int widthRatio = (int)Math.ceil(bmpFactoryOptions.
    outWidth/(float)dw);
    Log.v("HEIGHTRATIO",""+heightRatio);
    Log.v("WIDTHRATIO",""+widthRatio);
    // 如果两个比率都大于 1,
    // 那么图像的一条边将大于屏幕
    if (heightRatio > 1 && widthRatio > 1)
    {
    if (heightRatio > widthRatio)
    {
    // 若高度比率更大,则根据它缩放
    bmpFactoryOptions.inSampleSize = heightRatio;
    }
    else
    {
    // 若宽度比率更大,则根据它缩放
    bmpFactoryOptions.inSampleSize = widthRatio;
    }
    }
    //对它进行真正的解码
    bmpFactoryOptions.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeFile(imageFilePath,
    bmpFactoryOptions);
    // 显示图像
    imv.setImageBitmap(bmp);
    }
    }
    }
    
    
    
    
    上述代码需要下列 layout/main.xml 文件:
    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/ReturnedImageView" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView> </LinearLayout>
    
    
    
    
    
  • 相关阅读:
    数据库pubs
    当前目录中查找特定类型的文件
    DBHelper,ADO直接操作数据库,扩展DataTable操作数据裤的方法
    行记录次序+等差数列
    面试的通用要求
    zoj_3367Connect them
    hdoj_4198Quick out of the Harbour
    Win32常见异常
    hdoj_1026Ignatius and the Princess I
    移动 II
  • 原文地址:https://www.cnblogs.com/mamamia/p/8583040.html
Copyright © 2020-2023  润新知