我想要做的是,在一個活動中用SurfaceView
表示打開相機,並在其中預覽。點擊一個按鈕後,SurfaceView
停止/不可見,因此圖像捕捉將顯示在ImageView
的活動中。單擊按鈕時如何隱藏SurfaceView?
是如下圖:
所以我必須具有類似下面的XML定製相機的活動,
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<SurfaceView
android:id="@+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="@+id/showImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
<FloatingActionButton
android:id="@+id/cameraButton"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerHorizontal="true" />
</FrameLayout>
我想要做的是,當我點擊在浮動按鈕上,SurfaceView
消失,ImageView
可見,則攝像機拍攝的圖像顯示在ImageView
上。
所以我已經試過到目前爲止
public class CameraActivity extends AppCompatActivity implements
SurfaceHolder.Callback {
Camera mCamera;
Camera.PictureCallback jpegCallback;
ImageView showImage;
FloatingActionButton btnCamera;
SurfaceHolder surfaceHolder;
SurfaceView surfaceView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
btnCamera = (FloatingActionButton) findViewById(R.id.cameraButton);
surfaceView = (SurfaceView)findViewById(R.id.surface_view);
showImage = (ImageView)findViewById(R.id.showImage);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//here when click the camera button
//camera take photo
//surface view disappear
//preview image shown on image view
mCamera.takePicture(null,null,jpegCallback);
showImage.setVisibility(View.VISIBLE);
surfaceView.setVisibility(View.INVISIBLE);
}
});
jpegCallback = new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
FileOutputStream outputStream = null;
File file_image = getDirs();
if(!file_image.exists() && !file_image.mkdirs()){
Toast.makeText(getApplicationContext(),"Failed to save picture",Toast.LENGTH_LONG).show();
}
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String date = simpleDateFormat.format(new Date());
String photofile = "myphotos" + date+ ".jpg";
String file_name = file_image.getAbsolutePath() + "/" + photofile;
File picFile = new File(file_name);
Bitmap bitmap = null;
try{
outputStream = new FileOutputStream(picFile);
outputStream.write(data);
outputStream.close();
//here set the picture capture to the image view
//convert it to bitmap,setBitmap to the imageView
bitmap = decodeFile(picFile,10);
if(bitmap !=null){
showImage.setImageBitmap(bitmap);
Toast.makeText(CameraActivity.this,
"Picture Captured Successfully:", Toast.LENGTH_LONG)
.show();
}else {
Toast.makeText(CameraActivity.this,
"Failed to Capture the picture. kindly Try Again:",
Toast.LENGTH_LONG).show();
}
} catch (IOException e){
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"Picture saved",Toast.LENGTH_LONG).show();
refreshCamera();
}
};
}
private void refreshCamera() {
if(surfaceHolder.getSurface() == null){
return;
}
//stop the camera preview
try{
mCamera.stopPreview();
}catch (Exception e){
e.printStackTrace();
}
//start camera again
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
//open camera
try{
mCamera = Camera.open();
}catch (RuntimeException ex){
ex.printStackTrace();
}
Camera.Parameters parameters;
parameters = mCamera.getParameters();
parameters.setPreviewFrameRate(20);
parameters.setPreviewSize(352,288);
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
try{
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
refreshCamera();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
mCamera.stopPreview();
mCamera.release();
mCamera= null;
}
}
試圖在此之後,我現在得到的是,屏幕是完全空白。 SurfaceView
是不可見的,ImageView
預覽從相機拍攝的圖像未顯示出來。 但我檢查了file_name
和bitmap
在日誌中有價值。
所以,我試圖以不SurfaceView
設置爲不可見象下面這樣:
btnCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cameraImage();
showImage.setVisibility(View.VISIBLE);
//surfaceView.setVisibility(View.INVISIBLE);
}
});
圖像可以顯示在上表面視圖的頂部等的下方,但如果我設置面視圖爲不可見,整個屏幕顯示爲空白。
但我想表面視線中消失,只有ImageView的使用。
當我使surfaceView不可見時,屏幕變爲空白..屏幕上只顯示白色 – ken