2014-05-24 3 views
0

Android Camera API를 사용하여 사진을 찍으려고합니다. 내 활동에서 나는 카메라 미리보기를 위해 SurfaceView를 가지고 있으며, 카메라 obj와 Button은 스냅 샷을 취합니다. 다음은 내 활동에 관한 것입니다.Android Camera API ImageView에서 비트 맵을 표시하는 방법?

@SuppressLint("NewApi") 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_myactivity2); 
    button1 = (Button) findViewById(R.id.button1); 
    button1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      cameraObj.takePicture(shutterCallback, null, pictureCallback); 
     } 
    }); 

    surfaceView = (SurfaceView) findViewById(R.id.camera_surfaceview); 
    cameraObj = Camera.open(0); 
    SurfaceHolder holder = surfaceView.getHolder(); 
    holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    holder.addCallback(new SurfaceHolder.Callback() { 

     @Override 
     public void surfaceDestroyed(SurfaceHolder holder) { 
      //do nothing for now 
     } 

     @Override 
     public void surfaceCreated(SurfaceHolder holder) { 
      try { 
       cameraObj.setPreviewDisplay(holder); 
       cameraObj.startPreview(); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      cameraObj.startPreview(); 

     } 

     @Override 
     public void surfaceChanged(SurfaceHolder holder, int format, int width, 
       int height) { 

     } 
    }); 
}//end of onCreate 

다음은 내 Shutter Call Back 및 Picture Call Back 메서드입니다.

private Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() { 

    @Override 
    public void onShutter() { 


    } 
}; 

private Camera.PictureCallback pictureCallback = new Camera.PictureCallback() { 

    String filename = "test_"; 

    @Override 
    public void onPictureTaken(byte[] data, Camera camera) { 
     try { 

      FileOutputStream fios = openFileOutput(filename, Context.MODE_PRIVATE); 
      fios.write(data); 
      fios.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
}; 

내 질문에 어떻게 비트 맵 이미지를 다른 활동 또는 동일한 활동에서 imageView로 표시합니까?

답변

0

//CONVERTING IMAGE TO BITMAP 

/*public static Bitmap getBitmapFromURL(String xxx) { 
    try { 
     URL url = new URL(src); 
     HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.connect(); 
     InputStream input = connection.getInputStream(); 
     Bitmap myBitmap = BitmapFactory.decodeStream(input); 
     return myBitmap; 
    } catch (IOException e) { 
     e.printStackTrace(); 
     return null; 
    } 
}*/ 

    void create_bitmap(){ 
     //creating bitmap 
     Bitmap source = BitmapFactory.decodeResource(getResources(), 
     R.drawable.image1); 
     //calling doGreyScale 
     doGreyscale(source); 
    } 

    public static void doGreyscale(Bitmap src) { 
     // constant factors 
     final double GS_RED = 0.299; 
     final double GS_GREEN = 0.587; 
     final double GS_BLUE = 0.114; 

     // create output bitmap 
     Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig()); 
     // pixel information 
     int A, R, G, B; 
     int pixel; 

     // get image size 
     int width = src.getWidth(); 
     int height = src.getHeight(); 

     // scan through every single pixel 
     for(int x = 0; x < width; ++x) { 
      for(int y = 0; y < height; ++y) { 
       // get one pixel color 
       pixel = src.getPixel(x, y); 
       // retrieve color of all channels 
       A = Color.alpha(pixel); 
       R = Color.red(pixel); 
       G = Color.green(pixel); 
       B = Color.blue(pixel); 
       // take conversion up to one single value 
       R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B); 
       // set new pixel color to output bitmap 
       bmOut.setPixel(x, y, Color.argb(A, R, G, B)); 
      } 
     } 




     //converting bitmap object to show in imageview2 
      ImageView mImg; 
     mImg = (ImageView) findViewById(R.id.imageView2); 
      mImg.setImageBitmap(bmOut); 

    } 

} 
시도
관련 문제