优化RAW格式拍照

TempBranch
Matthew 9 months ago
parent 7a9824b4a6
commit 23c3a482ee

@ -727,6 +727,8 @@ Java_com_xypower_mpapp_MicroPhotoService_captureFinished(
if (res < 0 || info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) if (res < 0 || info.format != ANDROID_BITMAP_FORMAT_RGBA_8888)
{ {
} }
bool hardwareBitmap = (info.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0;
void* pixels = NULL; void* pixels = NULL;
res = AndroidBitmap_lockPixels(env, bitmap, &pixels); res = AndroidBitmap_lockPixels(env, bitmap, &pixels);
if (res < 0) if (res < 0)

@ -1439,13 +1439,13 @@ void CPhoneDevice::CloseCamera2(CPhoneDevice::CPhoneCamera* camera, unsigned int
delete camera; delete camera;
} }
XYLOG(XYLOG_SEVERITY_DEBUG, "TP: Will Turn Off Power=%u", photoId); XYLOG(XYLOG_SEVERITY_DEBUG, "TP: Will Turn Off Power PHOTOID=%u", photoId);
if (turnOffOtg) if (turnOffOtg)
{ {
TurnOffOtg(NULL); TurnOffOtg(NULL);
} }
TurnOffCameraPower(NULL); TurnOffCameraPower(NULL);
XYLOG(XYLOG_SEVERITY_DEBUG, "TP: End Turn Off Power=%u", photoId); XYLOG(XYLOG_SEVERITY_DEBUG, "TP: End Turn Off Power PHOTOID=%u", photoId);
XYLOG(XYLOG_SEVERITY_DEBUG, "TP: CloseCamera PHOTOID=%u", photoId); XYLOG(XYLOG_SEVERITY_DEBUG, "TP: CloseCamera PHOTOID=%u", photoId);
@ -1789,6 +1789,7 @@ bool CPhoneDevice::OnImageReady(cv::Mat& mat)
bool CPhoneDevice::OnCaptureReady(bool photoOrVideo, bool result, cv::Mat& mat, unsigned int photoId) bool CPhoneDevice::OnCaptureReady(bool photoOrVideo, bool result, cv::Mat& mat, unsigned int photoId)
{ {
XYLOG(XYLOG_SEVERITY_INFO, "RAW Capture finished: %u RES=%d", photoId, (result ? 1 : 0));
if (photoOrVideo) if (photoOrVideo)
{ {
if (result) if (result)
@ -1799,8 +1800,21 @@ bool CPhoneDevice::OnCaptureReady(bool photoOrVideo, bool result, cv::Mat& mat,
{ {
std::vector<IDevice::RECOG_OBJECT> objs; std::vector<IDevice::RECOG_OBJECT> objs;
TakePhotoCb(result, mPhotoInfo, "", time(NULL), objs); TakePhotoCb(result, mPhotoInfo, "", time(NULL), objs);
CPhoneCamera* pCamera = mCamera;
mCamera = NULL;
bool turnOffOtg = (mPhotoInfo.usbCamera != 0);
std::thread closeThread(&CPhoneDevice::CloseCamera2, this, pCamera, mPhotoInfo.photoId, turnOffOtg);
m_threadClose.swap(closeThread);
if (closeThread.joinable())
{
closeThread.detach();
} }
} }
}
return true;
} }
bool CPhoneDevice::OnVideoReady(bool photoOrVideo, bool result, const char* path, unsigned int photoId) bool CPhoneDevice::OnVideoReady(bool photoOrVideo, bool result, const char* path, unsigned int photoId)

@ -14,6 +14,7 @@ import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageDecoder; import android.graphics.ImageDecoder;
import android.location.Location; import android.location.Location;
import android.location.LocationListener; import android.location.LocationListener;
@ -405,14 +406,14 @@ public class MicroPhotoService extends Service {
@Override @Override
public void run() { public void run() {
Bitmap bm = null; Bitmap bm = null;
ImageDecoder.Source src = ImageDecoder.createSource(new File(path)); // ImageDecoder.Source src = ImageDecoder.createSource(new File(path));
try { try {
bm = ImageDecoder.decodeBitmap(src); // bm = ImageDecoder.decodeBitmap(src);
bm = BitmapFactory.decodeFile(path);
} catch (Exception ex) { } catch (Exception ex) {
} }
mService.captureFinished(mService.mNativeHandle, photoOrVideo, result && bm != null, bm, videoId);
} }
}); });

@ -551,12 +551,7 @@ public class RawActivity extends AppCompatActivity {
/** /**
* A {@link Handler} for showing {@link Toast}s on the UI thread. * A {@link Handler} for showing {@link Toast}s on the UI thread.
*/ */
private final Handler mMessageHandler = new Handler(Looper.getMainLooper()) { private final Handler mMessageHandler = new Handler(Looper.getMainLooper());
@Override
public void handleMessage(Message msg) {
}
};
@Override @Override
@ -584,19 +579,15 @@ public class RawActivity extends AppCompatActivity {
mCameraId = Integer.toString(intent.getIntExtra("cameraId", 0)); mCameraId = Integer.toString(intent.getIntExtra("cameraId", 0));
mOrientation = intent.getIntExtra("orientation", -1); mOrientation = intent.getIntExtra("orientation", -1);
int width = intent.getIntExtra("width", 0); int width = intent.getIntExtra("width", MAX_PREVIEW_WIDTH);
int height = intent.getIntExtra("height", 0); int height = intent.getIntExtra("height", MAX_PREVIEW_HEIGHT);
mImageSize = new Size(width, height); mImageSize = new Size(width, height);
mPhotoId = intent.getLongExtra("videoId", 0); mPhotoId = intent.getLongExtra("videoId", 0);
mMessageHandler.postDelayed(new Runnable() {
@Override mTextureView.setAspectRatio(width, height);
public void run() {
takePicture();
}
}, 400);
} }
@ -618,6 +609,13 @@ public class RawActivity extends AppCompatActivity {
if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) { if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) {
mOrientationListener.enable(); mOrientationListener.enable();
} }
mMessageHandler.postDelayed(new Runnable() {
@Override
public void run() {
takePicture();
}
}, 400);
} }
@Override @Override
@ -641,33 +639,27 @@ public class RawActivity extends AppCompatActivity {
return false; return false;
} }
try { try {
String[] cameraIds = manager.getCameraIdList();
// Find a CameraDevice that supports RAW captures, and configure state.
for (String cameraId : cameraIds) {
CameraCharacteristics characteristics CameraCharacteristics characteristics
= manager.getCameraCharacteristics(cameraId); = manager.getCameraCharacteristics(mCameraId);
int[] capabilities = characteristics.get( int[] capabilities = characteristics.get(
CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES); CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES);
// We only use a camera that supports RAW in this sample. // We only use a camera that supports RAW in this sample.
if (!contains(capabilities, if (!contains(capabilities,
CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_RAW)) { CameraCharacteristics.REQUEST_AVAILABLE_CAPABILITIES_RAW)) {
continue; return false;
}
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
if (facing != CameraMetadata.LENS_FACING_FRONT) {
continue;
} }
StreamConfigurationMap map = characteristics.get( StreamConfigurationMap map = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
// For still image captures, we use the largest available size. // For still image captures, we use the largest available size.
Size[] outputSizes = map.getOutputSizes(ImageFormat.JPEG);
Size largestJpeg = Collections.max( Size largestJpeg = Collections.max(
Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)), Arrays.asList(map.getOutputSizes(ImageFormat.JPEG)),
new CompareSizesByArea()); new CompareSizesByArea());
outputSizes = map.getOutputSizes(ImageFormat.RAW_SENSOR);
Size largestRaw = Collections.max( Size largestRaw = Collections.max(
Arrays.asList(map.getOutputSizes(ImageFormat.RAW_SENSOR)), Arrays.asList(map.getOutputSizes(ImageFormat.RAW_SENSOR)),
new CompareSizesByArea()); new CompareSizesByArea());
@ -678,25 +670,24 @@ public class RawActivity extends AppCompatActivity {
// using them are finished. // using them are finished.
if (mJpegImageReader == null || mJpegImageReader.getAndRetain() == null) { if (mJpegImageReader == null || mJpegImageReader.getAndRetain() == null) {
mJpegImageReader = new RefCountedAutoCloseable<>( mJpegImageReader = new RefCountedAutoCloseable<>(
ImageReader.newInstance(largestJpeg.getWidth(), ImageReader.newInstance(mImageSize.getWidth(),
largestJpeg.getHeight(), ImageFormat.JPEG, /*maxImages*/5)); mImageSize.getHeight(), ImageFormat.JPEG, /*maxImages*/5));
} }
mJpegImageReader.get().setOnImageAvailableListener( mJpegImageReader.get().setOnImageAvailableListener(
mOnJpegImageAvailableListener, mBackgroundHandler); mOnJpegImageAvailableListener, mBackgroundHandler);
if (mRawImageReader == null || mRawImageReader.getAndRetain() == null) { if (mRawImageReader == null || mRawImageReader.getAndRetain() == null) {
mRawImageReader = new RefCountedAutoCloseable<>( mRawImageReader = new RefCountedAutoCloseable<>(
ImageReader.newInstance(largestRaw.getWidth(), ImageReader.newInstance(mImageSize.getWidth(),
largestRaw.getHeight(), ImageFormat.RAW_SENSOR, /*maxImages*/ 5)); mImageSize.getHeight(), ImageFormat.RAW_SENSOR, /*maxImages*/ 5));
} }
mRawImageReader.get().setOnImageAvailableListener( mRawImageReader.get().setOnImageAvailableListener(
mOnRawImageAvailableListener, mBackgroundHandler); mOnRawImageAvailableListener, mBackgroundHandler);
mCharacteristics = characteristics; mCharacteristics = characteristics;
mCameraId = cameraId;
} }
return true; return true;
}
} catch (CameraAccessException e) { } catch (CameraAccessException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -854,6 +845,7 @@ public class RawActivity extends AppCompatActivity {
@Override @Override
public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) { public void onConfigureFailed(CameraCaptureSession cameraCaptureSession) {
// showToast("Failed to configure camera."); // showToast("Failed to configure camera.");
int aa = 0;
} }
}, mBackgroundHandler }, mBackgroundHandler
); );
@ -941,6 +933,9 @@ public class RawActivity extends AppCompatActivity {
// Find the rotation of the device relative to the native device orientation. // Find the rotation of the device relative to the native device orientation.
int deviceRotation = getWindowManager().getDefaultDisplay().getRotation(); int deviceRotation = getWindowManager().getDefaultDisplay().getRotation();
if (mOrientation > 0) {
deviceRotation = mOrientation - 1;
}
Point displaySize = new Point(); Point displaySize = new Point();
getWindowManager().getDefaultDisplay().getSize(displaySize); getWindowManager().getDefaultDisplay().getSize(displaySize);

Loading…
Cancel
Save