拍照瓦特/摄像机的意图和显示的ImageView或TextView的? [英] Take photo w/ camera intent and display in imageView or textView?

查看:195
本文介绍了拍照瓦特/摄像机的意图和显示的ImageView或TextView的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于如何使用相机的意图(或照相机API)来拍摄图像,然后将图像转换为ImageView的对我来说,在我的应用程序中显示的问题。这是我到目前为止所。

I have a question about how to take an image using the camera intent (or camera API) and then bring the image into an imageView for me to display in my application. This is what I have so far.

我设置一个按钮

Button btnPicture = (Button) findViewById(R.id.btn_picture);
    btnPicture.setOnClickListener(this);

我安装一个摄像头的方法

I setup a Camera method

private void Camera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, TAKE_PICTURE_CODE);
    intent.setAction(Intent.ACTION_GET_CONTENT);
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, REQUEST_CODE);
}

这是我在哪里丢失。我试图来处理我拍摄影像。

And this is where I am lost. I am trying to process the image that I took.

    private void processImage(Intent intent) {
    setContentView(R.layout.imagelayout);
    ImageView imageView = (ImageView)findViewById(R.id.image_view);
    cameraBitmap = (Bitmap)intent.getExtras().get("data");
    imageView.setImageBitmap(cameraBitmap);
}

我的目的是为了显示你把里面image_view图像。我没有收到一个错误,没有任何反应。当我拍照,我被要求要么拍摄另一张照片或之后我使用该设备后退按钮施加力关闭。看来,我取出我的应用程序的完全,和返回是一个大问题。有什么建议么?我在想什么?

My intent is to display the image that you took inside image_view. I am not receiving an error, nothing happens. When I take the picture, I am asked to either take another picture or after I use the device back button the application force closes. It seems that I am taken out of my application completely, and returning is a big issue. Any suggestions? What am I missing?

0是啊,这里是我的onActivityResult

O yea, and here is my onActivityResult

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if(TAKE_PICTURE_CODE == requestCode) {

        Bundle extras = data.getExtras();
        if (extras.containsKey("data")) {
            Bitmap bmp = (Bitmap) extras.get("data");
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] image = baos.toByteArray();
            if (image != null) {
                Log.d(TAG, "image != null");
            }

        } else {
            Toast.makeText(getBaseContext(), "Fail to capture image", Toast.LENGTH_LONG).show();
        }

    }
}

我试图把图像中getExtras,然后将其保存为ByteArray。是另一件事,我要怎样做。不知道这一切是如何走到一起。

I am trying to put the image in getExtras, and then store it to a ByteArray. Was another thing I was trying to do. Not sure how it all comes together.

推荐答案

我发现很容易和有益的方法是这样的:

The method which i found to be easy and helpful is this:

MainActivity

private static String root = null;
private static String imageFolderPath = null;        
private String imageName = null;
private static Uri fileUri = null;
private static final int CAMERA_IMAGE_REQUEST=1;

public void captureImage(View view) {

    ImageView imageView = (ImageView) findViewById(R.id.capturedImageview);

             // fetching the root directory
     root = Environment.getExternalStorageDirectory().toString()
     + "/Your_Folder";

     // Creating folders for Image
     imageFolderPath = root + "/saved_images";
     File imagesFolder = new File(imageFolderPath);
     imagesFolder.mkdirs();

    // Generating file name
    imageName = "test.png";

    // Creating image here

    File image = new File(imageFolderPath, imageName);

    fileUri = Uri.fromFile(image);

    imageView.setTag(imageFolderPath + File.separator + imageName);

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    startActivityForResult(takePictureIntent,
            CAMERA_IMAGE_REQUEST);

}

,然后在活动的 onActivityResult 方式:

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

    if (resultCode == RESULT_OK) {

        switch (requestCode) {
        case CAMERA_IMAGE_REQUEST:

            Bitmap bitmap = null;
            try {
                GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
                bitmap = getImageThumbnail.getThumbnail(fileUri, this);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            // Setting image image icon on the imageview

            ImageView imageView = (ImageView) this
                    .findViewById(R.id.capturedImageview);

            imageView.setImageBitmap(bitmap);

            break;

        default:
            Toast.makeText(this, "Something went wrong...",
                    Toast.LENGTH_SHORT).show();
            break;
        }

    }
}

GetImageThumbnail.java

public class GetImageThumbnail {

private static int getPowerOfTwoForSampleRatio(double ratio) {
    int k = Integer.highestOneBit((int) Math.floor(ratio));
    if (k == 0)
        return 1;
    else
        return k;
}

public Bitmap getThumbnail(Uri uri, Context context)
        throws FileNotFoundException, IOException {
    InputStream input = context.getContentResolver().openInputStream(uri);

    BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
    onlyBoundsOptions.inJustDecodeBounds = true;
    onlyBoundsOptions.inDither = true;// optional
    onlyBoundsOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
    BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
    input.close();
    if ((onlyBoundsOptions.outWidth == -1)
            || (onlyBoundsOptions.outHeight == -1))
        return null;

    int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight
            : onlyBoundsOptions.outWidth;

    double ratio = (originalSize > 400) ? (originalSize / 350) : 1.0;

    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
    bitmapOptions.inDither = true;// optional
    bitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// optional
    input = context.getContentResolver().openInputStream(uri);
    Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
    input.close();
    return bitmap;
}
}

,然后在的ImageView的onclick 方法是这样的:

public void showFullImage(View view) {
    String path = (String) view.getTag();

    if (path != null) {

        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri imgUri = Uri.parse("file://" + path);
        intent.setDataAndType(imgUri, "image/*");
        startActivity(intent);

    }

}

这篇关于拍照瓦特/摄像机的意图和显示的ImageView或TextView的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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