1. Are you ready for the Galaxy S20? Here is everything we know so far!

Camera Preview

Discussion in 'Android Development' started by sehudson, Aug 31, 2011.

  1. sehudson

    sehudson Newbie
    Thread Starter

    I have a camera activity in my app. Once I get the byte array back from onPicturetaken, I convert the byte array to a bitmap using bitmap factory. How do I take that bitmap and pass it to a previewer so the user can see the picture they took.
     



    1. Download the Forums for Android™ app!


      Download

       
  2. jonbonazza

    jonbonazza Android Expert

    Something like this:

    ImageActivity.java
    Code (Text):
    1.  
    2. public class ImageActivity extends Activity
    3. {
    4.     //extraneous details omitted...
    5.     //...
    6.  
    7.     byte[] picData;
    8.    
    9.     @Override
    10.     public void onCreate(Bundle savedInstanceState)
    11.     {
    12.          super.onCreate(savedInstanceState);
    13.    
    14.          byte[] picBytes = getIntent().getByteArrayExtra("picBytes");
    15.          
    16.          Bitmap bm = BitmapFactory.decodeByteArray(picBytes, 0, picBytes.length);
    17.  
    18.          setContentView(R.layout.main);
    19.  
    20.          ImageView image = (ImageView)findViewById(R.id.Image);
    21.          image.setImageBitmap(bm);
    22.          image.invalidate();
    23.     }        
    24.  
    main.xml
    Code (Text):
    1.  
    2. <?xml version="1.0" encoding="utf-8"?>
    3. <LinearLayout
    4.       xmlns:android="http://schemas.android.com/apk/res/android"
    5.       android:id="@+id/Layout"
    6.       android:orientation="vertical"
    7.       android:layout_width="fill_parent"
    8.       android:layout_height="fill_parent">
    9. <ImageView
    10.       android:id="@+id/Image"
    11.       android:layout_width="fill_parent"
    12.       android:layout_height="fill_parent" />
    13. </LinearLayout>
    14.  
    CameraActivity.java
    Code (Text):
    1.  
    2. public class CameraActivity extends Activity implements Camera.PictureCallback
    3. {
    4.        //Extraneous details omitted...
    5.        //...
    6.  
    7.        public void onPictureTaken(byte[] data, Camera cam)
    8.        {
    9.              Intent intent = new Intent(CameraActivity.this, ImageActivity.class);
    10.              intent.putExtra("picBytes", data);
    11.              startActivity(intent);
    12.              finish(); //Leave out to allow user to go back to camera
    13.        }
    14.  
    15.        //.....
    16. }
    17.  
     
Loading...

Share This Page

Loading...