【问题标题】:How to display image captured by camera to next screen in android如何在android中将相机捕获的图像显示到下一个屏幕
【发布时间】:2012-08-10 17:23:11
【问题描述】:

您好,我在 Android 中使用相机应用程序。我想将 PictureCallback 方法中的字节数据传递给另一个活动,并希望在该活动中显示它。我使用了以下代码:

Camera.PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            Intent i = new Intent(FbCamera.this, view.class);
               i.putExtra("photo", data);
               Log.d(TAG, "jpegCallback" +data);
               startActivity(i);
        }
    };

第二类view.class如下图,

public class view extends Activity {    
    private static final String TAG = "Camera";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main2);
        Bundle extras = getIntent().getExtras();
        byte[] photo = extras.getByteArray("photo");
        Log.i(TAG, "jpegCallback2" + photo);
        Bitmap bitmap  = BitmapFactory.decodeByteArray (photo, 0, photo.length);
        ImageView imgView = (ImageView)findViewById(R.id.photoResultView);
        imgView.setImageBitmap(bitmap);
    }
}

这是我的布局 main2.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"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ImageView android:layout_height="fill_parent" 
           android:id="@+id/photoResultView" 
           android:src="@drawable/icon" 
           android:layout_width="fill_parent"></ImageView>
</LinearLayout>

当我运行此代码时,force_close 在相机点击后出现。如果有人知道,请帮助我。

【问题讨论】:

    标签: android image bitmap


    【解决方案1】:

    此代码应该插入到您开始相机活动的位置......就像点击某个按钮时说......它会打开您的相机-

    public void takePhoto(View view) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(Environment.getExternalStorageDirectory(),  "Pic.jpg");
        //You save your image in your sdcard by name "Pic.jpg"
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);  //Save uri that is path of your image
        startActivityForResult(intent, TAKE_PICTURE);
    }
    

    然后您可以像这样覆盖 onActivityResult 方法 -

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case TAKE_PICTURE:
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedImage = imageUri; //Get image path                          
                getContentResolver().notifyChange(selectedImage, null);
                btn = (ImageButton) findViewById(R.id.camera);
                ContentResolver cr = getContentResolver();
                Bitmap bitmap;
                try {
                     bitmap = android.provider.MediaStore.Images.Media
                     .getBitmap(cr, selectedImage);
    
                    btn.setImageBitmap(bitmap); // I have displayed that image on imagebutton
                     // you can display anyware you want like imageview
                    Toast.makeText(this, selectedImage.toString(),
                            Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                            .show();
                    Log.e("Camera", e.toString());
                }
            }
        }
    
    }
    

    【讨论】:

    • 嗨 rohit,在我的应用程序中,我不想将图像保存到 sd 卡。我只想将捕获的图像传递给另一个活动并在那里显示。我做了如上所示的代码。在该代码中,我可以将图像的字节数据访问到第二个活动。我想将其转换为位图图像并希望在图像视图中显示它。但是会出现错误。你知道吗?
    • 很可能如果 decodeByteArray() 无法解码,那么它可能会出错..如果我发现了什么,我会告诉你.. :)
    • 您使用的是模拟器还是设备?如果可以的话,你应该在设备上尝试一下
    • 如果有什么对你有用..那么你能分享一下吗?否则你可以接受答案^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    • 2012-06-09
    • 2014-11-29
    • 2017-07-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-20
    相关资源
    最近更新 更多