android


android sending pictures from cam via socket


I transmit the images frame-by-frame from the camera via the socket after clicking on the form. I do not understand how to make an uninterrupted transfer of images after clicking on the form. Tried to put in a cycle, but in this case the image displayed on the form hangs. How to do it right? Here is the code with which I work.
/**
* Called when the user clicks on our {#code SurfaceView}, which has ID {#code mainSurfaceView}
* as defined in the {#code mainactivity.xml} layout file. <p>Captures a full-resolution image
* and saves it to permanent storage.</p>
*/
public void onClickOnSurfaceView(View v) {
if (mCaptureSession != null) {
try {
CaptureRequest.Builder requester =
mCamera.createCaptureRequest(mCamera.TEMPLATE_STILL_CAPTURE);
requester.addTarget(mCaptureBuffer.getSurface());
try {
// This handler can be null because we aren't actually attaching any callback
mCaptureSession.capture(requester.build(), /*listener*/null, /*handler*/null);
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to file actual capture request", ex);
}
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to build actual capture request", ex);
}
} else {
Log.e(TAG, "User attempted to perform a capture outside our session");
}
// Control flow continues in mImageCaptureListener.onImageAvailable()
}
/**
* Callbacks invoked upon state changes in our {#code SurfaceView}.
*/
final SurfaceHolder.Callback mSurfaceHolderCallback = new SurfaceHolder.Callback() {
/** The camera device to use, or null if we haven't yet set a fixed surface size. */
private String mCameraId;
/** Whether we received a change callback after setting our fixed surface size. */
private boolean mGotSecondCallback;
#Override
public void surfaceCreated(SurfaceHolder holder) {
// This is called every time the surface returns to the foreground
Log.i(TAG, "Surface created");
mCameraId = null;
mGotSecondCallback = false;
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i(TAG, "Surface destroyed");
holder.removeCallback(this);
// We don't stop receiving callbacks forever because onResume() will reattach us
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// On the first invocation, width and height were automatically set to the view's size
if (mCameraId == null) {
// Find the device's back-facing camera and set the destination buffer sizes
try {
for (String cameraId : mCameraManager.getCameraIdList()) {
CameraCharacteristics cameraCharacteristics =
mCameraManager.getCameraCharacteristics(cameraId);
if (cameraCharacteristics.get(cameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_BACK) {
Log.i(TAG, "Found a back-facing camera");
StreamConfigurationMap info = cameraCharacteristics
.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
// Bigger is better when it comes to saving our image
Size largestSize = Collections.max(
Arrays.asList(info.getOutputSizes(ImageFormat.JPEG)),
new CompareSizesByArea());
// Prepare an ImageReader in case the user wants to capture images
Log.i(TAG, "Capture size: " + largestSize);
mCaptureBuffer = ImageReader.newInstance(640,
480, ImageFormat.JPEG, /*maxImages*/2);
mCaptureBuffer.setOnImageAvailableListener(
mImageCaptureListener, mBackgroundHandler);
Log.i(TAG, "SurfaceView size: " +
mSurfaceView.getWidth() + 'x' + mSurfaceView.getHeight());
Size optimalSize = chooseBigEnoughSize(
info.getOutputSizes(SurfaceHolder.class), width, height);
// Set the SurfaceHolder to use the camera's largest supported size
Log.i(TAG, "Preview size: " + optimalSize);
SurfaceHolder surfaceHolder = mSurfaceView.getHolder();
surfaceHolder.setFixedSize(optimalSize.getWidth(),
optimalSize.getHeight());
mCameraId = cameraId;
return;
}
}
} catch (CameraAccessException ex) {
Log.e(TAG, "Unable to list cameras", ex);
}
Log.e(TAG, "Didn't find any back-facing cameras");
// This is the second time the method is being invoked: our size change is complete
} else if (!mGotSecondCallback) {
if (mCamera != null) {
Log.e(TAG, "Aborting camera open because it hadn't been closed");
return;
}
// Open the camera device
try {
mCameraManager.openCamera(mCameraId, mCameraStateCallback,
mBackgroundHandler);
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to configure output surface", ex);
}
mGotSecondCallback = true;
// Control flow continues in mCameraStateCallback.onOpened()
}
}
};
/**
* Calledbacks invoked upon state changes in our {#code CameraDevice}. <p>These are run on
* {#code mBackgroundThread}.</p>
*/
final CameraDevice.StateCallback mCameraStateCallback =
new CameraDevice.StateCallback() {
#Override
public void onOpened(CameraDevice camera) {
Log.i(TAG, "Successfully opened camera");
mCamera = camera;
try {
List<Surface> outputs = Arrays.asList(
mSurfaceView.getHolder().getSurface(), mCaptureBuffer.getSurface());
camera.createCaptureSession(outputs, mCaptureSessionListener,
mBackgroundHandler);
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to create a capture session", ex);
}
// Control flow continues in mCaptureSessionListener.onConfigured()
}
#Override
public void onDisconnected(CameraDevice camera) {
Log.e(TAG, "Camera was disconnected");
}
#Override
public void onError(CameraDevice camera, int error) {
Log.e(TAG, "State error on device '" + camera.getId() + "': code " + error);
}
};
/**
* Callbacks invoked upon state changes in our {#code CameraCaptureSession}. <p>These are run on
* {#code mBackgroundThread}.</p>
*/
final CameraCaptureSession.StateCallback mCaptureSessionListener =
new CameraCaptureSession.StateCallback() {
#Override
public void onConfigured(CameraCaptureSession session) {
Log.i(TAG, "Finished configuring camera outputs");
mCaptureSession = session;
SurfaceHolder holder = mSurfaceView.getHolder();
if (holder != null) {
try {
// Build a request for preview footage
CaptureRequest.Builder requestBuilder =
mCamera.createCaptureRequest(mCamera.TEMPLATE_PREVIEW);
requestBuilder.addTarget(holder.getSurface());
CaptureRequest previewRequest = requestBuilder.build();
// Start displaying preview images
try {
session.setRepeatingRequest(previewRequest, /*listener*/null,
/*handler*/null);
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to make repeating preview request", ex);
}
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to build preview request", ex);
}
} else {
Log.e(TAG, "Holder didn't exist when trying to formulate preview request");
}
}
#Override
public void onClosed(CameraCaptureSession session) {
mCaptureSession = null;
}
#Override
public void onConfigureFailed(CameraCaptureSession session) {
Log.e(TAG, "Configuration error on device '" + mCamera.getId());
}
};
/**
* Callback invoked when we've received a JPEG image from the camera.
*/
final ImageReader.OnImageAvailableListener mImageCaptureListener =
new ImageReader.OnImageAvailableListener() {
#Override
public void onImageAvailable(ImageReader reader) {
// Save the image once we get a chance
mBackgroundHandler.post(new CapturedImageSaver(reader.acquireNextImage()));
}
};
static class CapturedImageSaver implements Runnable {
/**
* The image to save.
*/
private Image mCapture;
public CapturedImageSaver(Image capture) {
mCapture = capture;
}
#Override
public void run() {
try {
// Choose an unused filename under the Pictures/ directory
File file = File.createTempFile(CAPTURE_FILENAME_PREFIX, ".jpg",
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES));
try (FileOutputStream ostream = new FileOutputStream(file)) {
Log.i(TAG, "Retrieved image is" +
(mCapture.getFormat() == ImageFormat.JPEG ? "" : "n't") + " a JPEG");
ByteBuffer buffer = mCapture.getPlanes()[0].getBuffer();
Log.i(TAG, "Captured image size: " +
mCapture.getWidth() + 'x' + mCapture.getHeight());
// Write the image out to the chosen file
byte[] jpeg = new byte[buffer.remaining()];
buffer.get(jpeg);
//ostream.write(jpeg);
//send image via socket
System.out.println(SERVER_IP);
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
System.out.println("Created serverAddr " + SERVER_IP);
Socket socket = new Socket(serverAddr, SERVER_PORT);
System.out.println("Socket created..");
System.out.println(mCapture.getWidth() + "^" + mCapture.getHeight());
try (DataOutputStream dOut = new DataOutputStream(socket.getOutputStream())) {
dOut.writeInt(jpeg.length);
dOut.write(jpeg);
dOut.flush();
}
} catch (FileNotFoundException ex) {
Log.e(TAG, "Unable to open output file for writing", ex);
} catch (IOException ex) {
Log.e(TAG, "Failed to write the image to the output file", ex);
}
} catch (IOException ex) {
Log.e(TAG, "Unable to create a new output file", ex);
} finally {
mCapture.close();
}
}
}
}
a bit remade code. The function added a call to receive a snapshot on the timer every 170 milliseconds. If I set the time less than the specified, the interface hangs ... Someone can tell me how to do it right? Now I have 5.88 frames per second. I would like to achieve at least 15 frames per second.
public void onClickOnSurfaceView(View v) {
int i = 0;
Timer myTimer = new Timer(); // Создаем таймер
final Handler uiHandler = new Handler();
myTimer.schedule(new TimerTask() { // Определяем задачу
#Override
public void run() {
uiHandler.post(new Runnable() {
#Override
public void run() {
if (mCaptureSession != null) {
//while(true){
try {
CaptureRequest.Builder requester =
mCamera.createCaptureRequest(mCamera.TEMPLATE_STILL_CAPTURE);
requester.addTarget(mCaptureBuffer.getSurface());
//while(i<20) {
try {
// This handler can be null because we aren't actually attaching any callback
mCaptureSession.capture(requester.build(), /*listener*/null, /*handler*/null);
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to file actual capture request", ex);
}
//}
} catch (CameraAccessException ex) {
Log.e(TAG, "Failed to build actual capture request", ex);
}
//}
} else {
Log.e(TAG, "User attempted to perform a capture outside our session");
}
}
});
};
}, 0L, 1L * 180); // интервал - 500 миллисекунд, 0 миллисекунд до первого запуска.
// Control flow continues in mImageCaptureListener.onImageAvailable()
}

Related Links

How to clean variables in an Activity?
Error in configuring virtual box to ADB
PhoneGap PushPlugin installation - Android
How get webview scale in Android 4
Transisting to different link when clicking on imageview android
Android, How to get/set layout parameters?
Unable to resume Activity..java.lang.NullPointerException in Log in Twitter
success in logcat but empty screen
please suggest any library for local db in android [closed]
Pattern for notifying events to Activities and Services at the same time
Holo theme and custom background for my button
Android compilation trouble after adding and removing BlackBerry SDK
How to Get All Memory Path (Internal or External Memory) list in android
Through Phone Gap how to know total size[used+unused] of Sd-card
How to set a colspan in an android gridview cell
i want to add icon in listfragmnet (headlines fragment) in it

Categories

HOME
cheerio
static
mysql-workbench
components
database-normalization
soa
settings
qpython
openstreetmap
drop-down-menu
dkim
pybossa
gruntfile
atlassian-stash
enterprise-miner
drive
libigl
named-entity-recognition
mongoid6
beta
dapper-extensions
fat
shared
has-and-belongs-to-many
progress-bar
spotipy
user-defined-functions
dotspatial
database-comparison
getlasterror
magento-1.9.2.4
dm-script
xdocreport
amazon-ses
cin
hackintosh
ogc
odoo-website
apriori
chronicle-map
tcc
builder
term
wcftestclient
spring-integration-sftp
er-diagram
reshape
java-websocket
cpu-cache
branch-prediction
rmongo
adafruit
delta
nupic
libvlc
computer-architecture
sapscript
matlab-deployment
bufferedinputstream
node-serialport
phpldapadmin
code-documentation
deform
rickshaw
matrix-inverse
menubar
synapse
syndicationfeed
spring-3
tlf
neos-server
process-explorer
abstract-data-type
jquery-1.9
sqlclr
wimax
onmousemove
fragmenttransaction
saleslogix
httponly
predicatebuilder
apache-commons-dbutils
node-redis
exponentiation
xui
jqueryform
sifr
spread
lxr
commercial-application

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App