繁体   English   中英

如何将相机捕获的图像显示到android中的下一个屏幕

[英]How to display image captured by camera to next screen in android

嗨,我在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 如果有人知道,请帮助我。

该代码应插入您开始进行摄像头活动的位置..例如,单击某些按钮时说。.它将打开您的摄像头,

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

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM