[英]Passing bitmap object to Mainactivity
i have a service in which my camera is taking a picture. 我有一项我的相机正在拍照的服务。 Here is the code: 这是代码:
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- My first question is would i get my image in the bitmap object here: ? 1-我的第一个问题是我将图像放在位图对象中吗?
Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
2- Secondly, i want to pass this object to the mainactivity. 2-其次,我想将此对象传递给mainactivity。 How will i do that?? 我该怎么做?
You can convert your image to a String and pass it to MainActivity. 您可以将图像转换为String并将其传递给MainActivity。
convert your image into a byte array though. 虽然将您的图像转换为字节数组。 Here's an example: 这是一个例子:
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();
And encode it to base64 : 并将其编码为base64:
String encodedImage = Base64.encodeToString(byte, Base64.DEFAULT);
byte[] bytearray = Base64.decode(encodedImage);
FileOutputStream imageOutFile = new FileOutputStream("after_convert.jpg");
imageOutFile.write(bytearray);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.