将位图对象传递给Mainactivity

时间:2014-12-26 22:22:45

标签: java android service bitmap

我有一项服务,我的相机正在拍照。这是代码:

public class Picture_Service extends Service 
{
      //Camera variables
      //a surface holder
      private SurfaceHolder sHolder;  
      //a variable to control the camera
      private Camera mCamera;
      //the camera parameters
      private Parameters parameters;
      private int i = 0;

private static final int DISCOVER_DURATION = 300; 
// our request code (must be greater than zero) 
private static final int REQUEST_BLU = 1; 

      /** Called when the activity is first created. */
    @Override
    public void onCreate() 
    {
        super.onCreate();
    }
    @Override
    public void onStart(Intent intent, int startId) {
      // TODO Auto-generated method stub
      super.onStart(intent, startId);
       mCamera = Camera.open();
       SurfaceView sv = new SurfaceView(getApplicationContext());
       try {
                  mCamera.setPreviewDisplay(sv.getHolder());
                  parameters = mCamera.getParameters();
                   //set camera parameters
                 mCamera.setParameters(parameters);
                 mCamera.startPreview();
                 mCamera.takePicture(null, null, mCall);
            } catch (IOException e) {
                  // TODO Auto-generated catch block
                Toast.makeText(this, "Logged in as Administrator!",Toast.LENGTH_LONG).show();
                Log.w("Display Hona Chahye", "Kr dia na display Rami");
                  e.printStackTrace();
            }
       //Get a surface
         sHolder = sv.getHolder();
        //tells Android that this surface will have its data constantly replaced
         sHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    }
    Camera.PictureCallback mCall = new Camera.PictureCallback() 
    {
       public void onPictureTaken(byte[] data, Camera camera) 
       {
          //decode the data obtained by the camera into a Bitmap
             FileOutputStream outStream = null;
                  try{
                      i++;
                      Log.w("Rami Rami","maha harami");
                      Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
                      //outStream = new FileOutputStream("/sdcard/Image.jpg");
                     //outStream.write(data);
                     //Bitmap yourSelectedImage = BitmapFactory.decodeFile("/sdcard/Image.jpg");
                     //BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); 
                     Log.w("Rami insan","Rami write hogya");
                     mCamera.release();
                      outStream.close();
                  } catch (FileNotFoundException e){
                      Log.d("CAMERA", e.getMessage());
                  } catch (IOException e){
                      Log.d("CAMERA", e.getMessage());
                  }
       }
    };
      @Override
      public IBinder onBind(Intent intent) {
            // TODO Auto-generated method stub
            return null;
      }
}

1-我的第一个问题是我会在位图对象中获取我的图像:?

Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);

2-其次,我想将此对象传递给mainactivity。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您可以将图像转换为字符串并将其传递给MainActivity。

将图像转换为字节数组。这是一个例子:

Bitmap bm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();

bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object

byte[] b = baos.toByteArray();

并将其编码为base64:

String encodedImage = Base64.encodeToString(byte, Base64.DEFAULT);

byte[] bytearray = Base64.decode(encodedImage);

FileOutputStream imageOutFile = new FileOutputStream("after_convert.jpg");
imageOutFile.write(bytearray);