无法使用 camerakit 拍照
unable to take picture with camerakit
我正在关注此文档 http://docs.camerakit.website/#/
我正在尝试拍照,但找不到确切的代码。
当我在我的 onCreate 方法中编写此代码时 android 工作室对我说这不是正确的代码:
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
camera.captureImage();
这是我的全部代码:
public class MainActivity extends AppCompatActivity {
CameraView cameraView;
ImageView img_photo;
Bitmap photo;
Button btt_scatta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Get root view from Activity
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
cameraView = (CameraView) findViewById(R.id.camera);
cameraView.setFacing(CameraKit.Constants.FACING_FRONT);
btt_scatta = (Button) findViewById(R.id.btt_scatta);
img_photo = (ImageView) findViewById(R.id.img_photo);
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
btt_scatta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
photo = getScreenShot(rootView);
img_photo.setImageBitmap(photo);
}
});
}
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
//Capture the root view
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
//Store the Bitmap into the phone
public static void store(Bitmap bm, String fileName){
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我已经遵循了整个代码,但没有任何结果。您可以在 github 网站找到代码:https://github.com/CameraKit/camerakit-android
我的范围是创建一个应用程序,从您的相机拍摄照片,然后在 Imageview 中向您显示照片。
我想说相机在这段代码下工作正常。我的相机没有任何问题,但只是在拍照的那一刻。
谢谢大家。
你应该打电话给
camera.captureImage()
在您 btt_scatta 的点击侦听器中。
我正在关注此文档 http://docs.camerakit.website/#/ 我正在尝试拍照,但找不到确切的代码。 当我在我的 onCreate 方法中编写此代码时 android 工作室对我说这不是正确的代码:
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
camera.captureImage();
这是我的全部代码:
public class MainActivity extends AppCompatActivity {
CameraView cameraView;
ImageView img_photo;
Bitmap photo;
Button btt_scatta;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//Get root view from Activity
final View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
cameraView = (CameraView) findViewById(R.id.camera);
cameraView.setFacing(CameraKit.Constants.FACING_FRONT);
btt_scatta = (Button) findViewById(R.id.btt_scatta);
img_photo = (ImageView) findViewById(R.id.img_photo);
camera.setCameraListener(new CameraListener() {
@Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
btt_scatta.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
photo = getScreenShot(rootView);
img_photo.setImageBitmap(photo);
}
});
}
@Override
protected void onResume() {
super.onResume();
cameraView.start();
}
@Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
//Capture the root view
public static Bitmap getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
return bitmap;
}
//Store the Bitmap into the phone
public static void store(Bitmap bm, String fileName){
final String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Screenshots";
File dir = new File(dirPath);
if(!dir.exists())
dir.mkdirs();
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
我已经遵循了整个代码,但没有任何结果。您可以在 github 网站找到代码:https://github.com/CameraKit/camerakit-android 我的范围是创建一个应用程序,从您的相机拍摄照片,然后在 Imageview 中向您显示照片。 我想说相机在这段代码下工作正常。我的相机没有任何问题,但只是在拍照的那一刻。 谢谢大家。
你应该打电话给
camera.captureImage()
在您 btt_scatta 的点击侦听器中。