I am new to Android development and this is my first app. I have found questions from other people very helpful in the past, but this time I couldn't find anything relevant to my question.So I decided to make my own.
I am trying to zoom in and out of the camera surface-view. I have tried many different sources and also looked at many questions on StackOverflow - none of them able to solve my problem.
My 'Main Activity' is provided below and if anything further is needed please ask. Thank you in advance.
package arpx.zilo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.hardware.Camera.OnZoomChangeListener;
import android.widget.ZoomControls;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CameraActivity extends Activity {
private static final String TAG = "ARPx.Zilo.CameraActivity.java";
Preview preview;
Button buttonClick;
Camera camera;
Activity act;
Context ctx;
Camera.Parameters params;
int zoom = 0;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
act = this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_my);
preview = new Preview(this, (SurfaceView) findViewById(R.id.surfaceView));
preview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
((FrameLayout) findViewById(R.id.content)).addView(preview);
preview.setKeepScreenOn(true);
//Button configurations
final Button help = (Button) findViewById(R.id.help);
{
help.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(CameraActivity.this, arpx.zilo.help.class);
startActivity(intent);
}
});
}
buttonClick = (Button) findViewById(R.id.capture);
buttonClick.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
preview.mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
buttonClick.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View arg0) {
camera.autoFocus(new Camera.AutoFocusCallback() {
@Override
public void onAutoFocus(boolean arg0, Camera arg1) {
camera.takePicture(shutterCallback, rawCallback, jpegCallback);
}
});
return true;
}
});
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
// increase your zoom
Camera.Parameters p = camera.getParameters();
if (p.isZoomSupported() == true)
{
int maxZoom = p.getMaxZoom();
if(zoom < maxZoom){
zoom++;
camera.startSmoothZoom(zoom);
}
camera.setParameters(p);
Log.d(TAG, "THIS IS SUPPOSED TO BE THE CURRENT ZOOM: " + zoom);
Log.d(TAG, "THIS IS THE MAX ZOOM FOR THE DEVICE: " + maxZoom);
}
return true;
}
else if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
// decrease your zoom
Camera.Parameters p = camera.getParameters();
if (p.isZoomSupported() == true) {
if(zoom > 0)
{
zoom--;
camera.startSmoothZoom(zoom);
}
camera.setParameters(p);
Log.d(TAG, "THIS IS SUPPOSED TO BE THE CURRENT ZOOM: " + zoom);
// Log.d(TAG, "THIS IS THE MAX ZOOM FOR THE DEVICE: " + maxZoom);
}
return true;
}
return super.onKeyUp(keyCode, event);
}
@Override
protected void onResume() {
super.onResume();
// preview.camera = Camera.open();
camera = Camera.open();
camera.startPreview();
preview.setCamera(camera);
}
@Override
protected void onPause() {
if(camera != null) {
camera.stopPreview();
preview.setCamera(null);
camera.release();
camera = null;
}
super.onPause();
}
private void resetCam() {
camera.startPreview();
preview.setCamera(camera);
}
private void refreshGallery(File file) {
Intent mediaScanIntent = new Intent( Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(Uri.fromFile(file));
sendBroadcast(mediaScanIntent);
}
ShutterCallback shutterCallback = new ShutterCallback() {
public void onShutter() {
// Log.d(TAG, "onShutter'd");
}
};
PictureCallback rawCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
// Log.d(TAG, "onPictureTaken - raw");
}
};
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
new SaveImageTask().execute(data);
resetCam();
Log.d(TAG, "onPictureTaken - jpeg");
}
};
private class SaveImageTask extends AsyncTask<byte[], Void, Void> {
@Override
protected Void doInBackground(byte[]... data) {
FileOutputStream outStream = null;
// Write to SD Card
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/Zilo");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
outStream.write(data[0]);
outStream.flush();
outStream.close();
Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length + " to " + outFile.getAbsolutePath());
refreshGallery(outFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
}
return null;
}
}
}
0 comments:
Post a Comment