2013-04-25 74 views
1

我有一个用于使用摄像头拍摄图像的表面视图类,我想知道如何将该图像发回给调用它的类。我试图通过意图发回图像不知道,如果我正确实施这一点。我有主类onActivityResult如下:发送表面视图摄像头图像返回到父级活动

//WHAT TO DO WITH RESULT DATA FROM CAMERA 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == CAMERA_PIC_REQUEST && picCount < 5) { 
     //curPic = (Bitmap) data.getExtras().get("data"); 
     curPic = (Bitmap) data.getExtras().get("files"); 
     ImageView images = new ImageView(getApplicationContext()); 

     //SET PICTURE TO NEW VIEW AND ANIMATE INTO POSITION 
     images.setImageBitmap(curPic); 


     //ADD TO PREVPICS LAYOUT 
     images.setPadding(3, 0, 0, 0); 
     showCase.addView(images); 
     badge.setImageBitmap(curPic); 

     //ADDS CLICK LISTENER TO EACH ELEMENT AND SETS ID 
     try{  
      Log.i("AFTER TAKING PIC", "PICCOUNT IS NOW:"+picCount); 
      showCase.getChildAt(picCount).setId(picCount); 
      images.setId(picCount); 
      showCase.getChildAt(picCount).setOnClickListener(btnListener);    
      images.setTag("pics"); 

      //SAVE PITCTURE 
      savePic(curPic); 

      previewImages(picCount); 
     }catch(Exception e){Log.e("ERROR TAKING PIC", e.toString());} 

     }else 
      Toast.makeText(getApplicationContext(), "Unable to add more pictures", Toast.LENGTH_SHORT).show(); 

} 

而且surfaceview类,如下所示:

public class SecondCamera extends Activity implements SurfaceHolder.Callback { 

Camera camera; 
SurfaceView surfaceView; 
SurfaceHolder surfaceHolder; 
boolean previewing = false; 
LayoutInflater controlInflater = null; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.second_cam); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 

    getWindow().setFormat(PixelFormat.UNKNOWN); 
    surfaceView = (SurfaceView) findViewById(R.id.surfaceView1); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    controlInflater = LayoutInflater.from(getBaseContext()); 
    /* 
    * View viewControl = controlInflater.inflate(R.layout.control, null); 
    * LayoutParams layoutParamsControl = new 
    * LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    * this.addContentView(viewControl, layoutParamsControl); 
    */ 

    Button buttonTakePicture = (Button) findViewById(R.id.button1); 
    buttonTakePicture.setOnClickListener(new Button.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      camera.takePicture(myShutterCallback, myPictureCallback_RAW, 
        myPictureCallback_JPG); 
     } 
    }); 
} 

ShutterCallback myShutterCallback = new ShutterCallback() { 

    @Override 
    public void onShutter() { 

    } 
}; 

PictureCallback myPictureCallback_RAW = new PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] arg0, Camera arg1) { 

    } 
}; 

PictureCallback myPictureCallback_JPG = new PictureCallback() { 

    @Override 
    public void onPictureTaken(byte[] rawImg, Camera arg1) { 
     Bitmap bitmapPicture = BitmapFactory.decodeByteArray(rawImg, 0, rawImg.length); 

     try { 

      Intent cameraIntent = new Intent(); 
      cameraIntent.putExtra("files", bitmapPicture); 
      setResult(123, cameraIntent); 
      finish(); 
     } catch (Exception e) { 
      Log.e("IMAGE CONVERT", e.toString()); 
     } 
    } 
}; 

@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    if (previewing) { 
     camera.stopPreview(); 
     previewing = false; 
    } 

    if (camera != null) { 
     try { 
      camera.setPreviewDisplay(surfaceHolder); 
      camera.startPreview(); 
      previewing = true; 
     } catch (IOException e) { 

      e.printStackTrace(); 
     } 
    } 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 

    camera = Camera.open(); 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 

    camera.stopPreview(); 
    camera.release(); 
    camera = null; 
    previewing = false; 
} 

}

回答

0

AFAIK,你不能直接传递一个Bitmap通过Intent。我想你想保存在某个地方,在本地存储中说。您可以使用File方法保存它。您可以通过IntentString的形式传递该文件路径,并使用它在您之前的Activity中访问它。

另一种选择可能是将其存储在一个ArrayList和传递,作为一个Serializable

+0

感谢您的回复将调查经过的路径。 – kabuto178 2013-04-25 03:34:45

+0

然后你又可以传递'byte []'值了,我也不能传递这个值吗? – kabuto178 2013-04-25 03:42:16

+0

我想你可以,但我认为存储图像和传递文件路径名是我知道的最佳选择。据我所知,您不应该通过“意图”传递大量数据。选中此[Google网上论坛帖子](https://groups.google.com/forum/?fromgroups=#!topic/android-developers/3sEonjD0Nqk) – codeMagic 2013-04-25 03:45:59