我正在尝试使用Android Camera API拍照。在我的活动中,我有SurfaceView用于相机预览,Camera obj和Button可以轻松实现。以下是我的活动创作:
@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
以下是我的快门回叫和图片回叫方法。
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 :(得分: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);
}
}