Android 2.3.3 Emulator doesn't recognize focus modes in camera.
public class LayerCounterActivity extends Activity {
private Camera mCamera;
private CameraPreview mPreview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (checkCameraHardware(getBaseContext())){
Toast.makeText(this, "Camera OK!", Toast.LENGTH_LONG).show();
mCamera=getCameraInstance();
if (mCamera==null){
Toast.makeText(this, "Camera not ready!", Toast.LENGTH_LONG).show();
}
else {
Camera.Parameters params = mCamera.getParameters();
List<String> focusModes = params.getSupportedFocusModes();
Why focusModes is null? When I start the application on a real device, everything works, but Emulator throws null pointer exception.
Related
I want to take a frame image from my TextureView (a preview form camera) when pressing a button. How can I do it?
I create an Activity with a TextureView and two buttons, one for start and stop the camera previewing and one for taking a frame from the preview.
This is my Activity:
public class PreviewActivity : Activity
{
bool myPreviewing;
Camera myCamera;
TextureView myTextureView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Preview);
Button startPreviewButton = FindViewById<Button>(Resource.Id.startPreview);
Button saveImageButton = FindViewById<Button>(Resource.Id.saveImage);
myTextureView = FindViewById<TextureView>(Resource.Id.myTextureView);
startPreviewButton.Click += delegate {
try
{
if (!myPreviewing)
{
myCamera = Open();
myCamera.SetPreviewTexture(myTextureView.SurfaceTexture);
myCamera.SetDisplayOrientation(90);
myCamera.StartPreview();
}
else
{
myCamera.StopPreview();
myCamera.Release();
}
}
catch (Java.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
myPreviewing = !myPreviewing;
}
};
saveImageButton.Click += delegate {
// do something for take frame
};
}
}
I am new to android and currently working on an app which uses the camera API. I am having trouble with the implementation of Zoom function in the app. I am using Camera kit API to make the app and Camera-kit Api does not provide support for zoom controls. Please help me with the code required. I am unable to figure it out since a week. I need to implement Zoom to the camera Preview.
this is my Code.
public class MicroscopeAcitvity extends AppCompatActivity implements View.OnClickListener {
CameraView cameraView;
ImageButton cameraTakePic;
ImageButton buttonFlash;
Camera mCamera; //= Camera.open();
Camera.Parameters params;
TextView T1,T2;
int counter = 0;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_microscope_acitvity);
cameraView = (CameraView) findViewById(R.id.camera);
cameraTakePic = (ImageButton) findViewById(R.id.button_take_picture);
cameraTakePic.setOnClickListener(this);
buttonFlash = (ImageButton) findViewById(R.id.button_Flash);
buttonFlash.setOnClickListener(this);
T1 = (TextView) findViewById(R.id.flash_text1);
T1.setOnClickListener(this);
//T1.setVisibility(View.GONE);
T2 = (TextView) findViewById(R.id.flash_text2);
T2.setOnClickListener(this);
//T2.setVisibility(View.GONE);
cameraView.setCameraListener(new CameraListener() {
#Override
public void onPictureTaken(byte[] picture) {
super.onPictureTaken(picture);
// Create a bitmap
Bitmap result = BitmapFactory.decodeByteArray(picture, 0, picture.length);
}
});
cameraView.setFocus(CameraKit.Constants.FOCUS_TAP);
}
#Override
protected void onResume() {
super.onResume();
cameraView.start();
}
#Override
protected void onPause() {
cameraView.stop();
super.onPause();
}
#Override
public void onClick(View v) {
int id;
id = v.getId();
if(id == R.id.button_take_picture){
cameraView.captureImage();
}
if(id == R.id.button_Flash){
if(counter % 2 != 0){
T1.setVisibility(View.VISIBLE);
T2.setVisibility(View.VISIBLE);
counter = counter+1;
}
else{
T1.setVisibility(View.GONE);
T2.setVisibility(View.GONE);
counter = counter+1;
}
}
if(id == R.id.flash_text1){
T1.setTextColor(Color.RED);
T2.setTextColor(Color.DKGRAY);
cameraView.setFlash(CameraKit.Constants.FLASH_ON);
}
if (id == R.id.flash_text2){
T2.setTextColor(Color.RED);
T1.setTextColor(Color.DKGRAY);
cameraView.setFlash(CameraKit.Constants.FLASH_OFF);
}
}
}
If you want to zoom by pinch you have to implement a ScaleGestureDetector.SimpleOnScaleGestureListener
To set the zoom you simply have to change the camera parameters
Camera.Parameters parameters = camera.getParameters();
parameters.setZoom(zoomFactorValue);
camera.setParameters(parameters);
if you have some time you can code the rest yourself or look up how he did it
http://opencamera.sourceforge.net/
He uses pinch zoom on a surfaceview (but his code is not efficient and really ugly, very hard to understand on some points)
I am trying to build an app that reads a QRCode from an IDCard that everyone on my company will use.
I would like to use the front camera as default when I run the app. I managed to do it with the rear camera but ideally I don't want to have to use a button to switch.
MainActivity:
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler {
private static final int REQUEST_CAMERA = 1;
private ZXingScannerView mScannerView;
Passageiros mPassageiro; //
Collection<Passageiros> listaPassageiros;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mScannerView = new ZXingScannerView(this){
#Override
protected IViewFinder createViewFinderView(Context context) {
return new CustomZXingScannerView(context);
}
};
List<BarcodeFormat> formats = new ArrayList<>();
formats.add(BarcodeFormat.QR_CODE);
setContentView(mScannerView);
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.M) {
if (checkPermission()) {
Toast.makeText(getApplicationContext(), "Permission already granted", Toast.LENGTH_LONG).show();
} else {
requestPermission();
}
}
}
...
}
How can I do this?
Looking at the sources of ZXing library
ZXingCameraView is extending BarcodeScannerView, which has a private CameraWrapper which you can set with setupLayout method. CameraWrapper allows you to initialize com.android.Camera.
And how to choose necessary out of available cameras is shown in that question: How do I open the "front camera" on the Android platform?
Edit: Actually there is even a ZXing CameraUtils class selecting camera on the back of device. Just change it to return id of front camera and pass that id to startCamera(frontCameraId) on your ZXingCameraView.
I am creating a flashlight app. In my code, if camera(hardware) is there, it should show an alert, but it does not show the alert. My code is as below:
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
but i get an error on
showNoCameraAlert();
Is this an invalid tag? or the tutorial i am using is old? I am using android studio. Here is my full Java code:
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
boolean isCameraFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!isCameraFlash) {
showNoCameraAlert();
} else {
camera = Camera.open();
params = camera.getParameters();
}
}
}
Any help will be appreciated. Thanks :)
You are calling the showNoCameraAlert() method. You need to add it to your MainActivity (that is the file where you put the code in.) So add it like this:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
The full code for your MainActivity:
package com.example.administrator.flashlight;
import ...;
public class MainActivity extends ActionBarActivity {
private Camera camera;
ImageButton flashlightSwitchImg;
private boolean isFlashlightOn;
Parameters params;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flashlightSwitchImg = (ImageButton) findViewById(R.id.flashlightSwitch);
//check if phone has a flashlight
boolean isCameraFlash = getApplicationContext().getPackageManager() .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)
if (!isCameraFlash) {
//show error ( showNoCamereAlert() is below)
showNoCameraAlert();
} else {
//open the camera
camera = Camera.open();
params = camera.getParameters();
}
}
private void showNoCameraAlert(){
//Show Error toast
Toast.makeText(getApplicationContext(), "Camera flashlight not available in this Android device!", Toast.LENGTH_SHORT).show();
}
}
I'm starting to think that you didn't implement the showNoCameraAlert() method.
Try adding something like this to your MainActivity class:
private void showNoCameraAlert(){
Toast.makeText(getApplicationContext(), "There's no camera on the device or it doesn't have a flash", Toast.LENGTH_SHORT).show();
}
New to android programming, writing an application that continuously streams image from camera and do some operation on and post the result on screen. My camera class is separate from the main activity class. I have a queue for captured images, two threads that reads and writes to the queue. I am having problems possibly with the initializations. I am not sure if I am using the CameraPreview correctly.
Here is my code.
public class Goggle extends Activity{
ImageQueue imageQueue = new ImageQueue();
ImageCapture capture;
private ThreadProcess thread;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
......
capture = new ImageCapture(this,imageQueue);
capture.start();
thread = new ThreadProcess();
thread.start();
}
private class ThreadProcess extends Thread {
...
}
}
public class ImageCapture extends Thread implements Camera.PreviewCallback{
private ImageQueue imageQueue;
private ImageUInt8 image;
private Activity parent;
private CameraPreview mPreview;
private Camera mCamera;
public ImageCapture (Activity parnt, ImageQueue queue) {
this.parent = parnt;
this.imageQueue = queue;
mPreview = new CameraPreview(parent,this,true);
FrameLayout preview = (FrameLayout) parent.findViewById(R.id.camera_preview);
preview.addView(mPreview);
setUpAndConfigureCamera();
}
/**
* Called each time a new image arrives in the data stream.
*/
#Override
public void onPreviewFrame(byte[] bytes, Camera camera) {
// convert from NV21 format into gray scale
synchronized (lockImage) {
ConvertNV21.nv21ToGray(bytes,image.width,image.height,image);
}
}
#Override
public void run() {
while (true) {
synchronized (lockImage) {
ImageData imageData = new ImageData(image);
imageQueue.pushImage(imageData);
}
notifyAll();
}
}
}