优化RAW格式照片的处理

TempBranch
Matthew 9 months ago
parent 965c3dbd5e
commit 1fdbc1701e

@ -730,15 +730,34 @@ Java_com_xypower_mpapp_MicroPhotoService_captureFinished(
bool hardwareBitmap = (info.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0; bool hardwareBitmap = (info.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0;
void* pixels = NULL; void* pixels = NULL;
res = AndroidBitmap_lockPixels(env, bitmap, &pixels); AHardwareBuffer* hardwareBuffer = NULL;
if (res < 0) if (hardwareBitmap)
{ {
} #if 0
res = AndroidBitmap_getHardwareBuffer(env, bitmap, &hardwareBuffer);
int32_t fence = -1;
res = AHardwareBuffer_lock(hardwareBuffer, AHARDWAREBUFFER_USAGE_CPU_READ_OFTEN, fence, NULL, &pixels);
cv::Mat mat(info.height, info.width, CV_8UC4, pixels);
AHardwareBuffer_unlock(hardwareBuffer, &fence);
cv::Mat mat(info.height, info.width, CV_8UC4, pixels); cv::cvtColor(mat, mat, cv::COLOR_RGB2BGR);
AndroidBitmap_unlockPixels(env, bitmap);
((CPhoneDevice *)dev)->OnCaptureReady(photoOrVideo != JNI_FALSE, result != JNI_FALSE, mat, (unsigned long)photoId);
#endif // 0
} else
{
res = AndroidBitmap_lockPixels(env, bitmap, &pixels);
cv::Mat tmp(info.height, info.width, CV_8UC4, pixels);
((CPhoneDevice *)dev)->OnCaptureReady(photoOrVideo != JNI_FALSE, result != JNI_FALSE, mat, (unsigned long)photoId); cv::Mat mat(info.height, info.width, CV_8UC4);
// tmp.copyTo(mat);
cv::cvtColor(tmp, mat, cv::COLOR_RGBA2BGR);
AndroidBitmap_unlockPixels(env, bitmap);
((CPhoneDevice *)dev)->OnCaptureReady(photoOrVideo != JNI_FALSE, result != JNI_FALSE, mat, (unsigned long)photoId);
}
} }
} }

@ -38,6 +38,7 @@ import android.os.PowerManager;
import android.os.RemoteException; import android.os.RemoteException;
import android.os.SystemClock; import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationCompat;
import androidx.core.content.FileProvider; import androidx.core.content.FileProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -406,12 +407,22 @@ 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));
try { try {
// bm = ImageDecoder.decodeBitmap(src); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
bm = BitmapFactory.decodeFile(path); ImageDecoder.Source src = ImageDecoder.createSource(new File(path));
ImageDecoder.OnHeaderDecodedListener listener =
new ImageDecoder.OnHeaderDecodedListener(){
@Override
public void onHeaderDecoded(@NonNull ImageDecoder decoder, @NonNull ImageDecoder.ImageInfo info, @NonNull ImageDecoder.Source source) {
decoder.setAllocator(ImageDecoder.ALLOCATOR_SOFTWARE);
}
};
bm = ImageDecoder.decodeBitmap(src, listener);
} else {
bm = BitmapFactory.decodeFile(path);
}
} catch (Exception ex) { } catch (Exception ex) {
} }
mService.captureFinished(mService.mNativeHandle, photoOrVideo, result && bm != null, bm, videoId); mService.captureFinished(mService.mNativeHandle, photoOrVideo, result && bm != null, bm, videoId);
} }

@ -8,6 +8,7 @@ import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.graphics.ImageDecoder; import android.graphics.ImageDecoder;
import android.media.midi.MidiDevice; import android.media.midi.MidiDevice;
import android.os.Build;
import android.os.Looper; import android.os.Looper;
import android.os.Message; import android.os.Message;
import android.os.SystemClock; import android.os.SystemClock;
@ -52,6 +53,8 @@ import android.util.Size;
import android.util.SparseIntArray; import android.util.SparseIntArray;
import android.view.Surface; import android.view.Surface;
import android.view.TextureView; import android.view.TextureView;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast; import android.widget.Toast;
import com.xypower.mpapp.MicroPhotoService; import com.xypower.mpapp.MicroPhotoService;
@ -167,14 +170,38 @@ public class RawActivity extends AppCompatActivity {
private final TextureView.SurfaceTextureListener mSurfaceTextureListener private final TextureView.SurfaceTextureListener mSurfaceTextureListener
= new TextureView.SurfaceTextureListener() { = new TextureView.SurfaceTextureListener() {
private boolean mTaken = false;
@Override @Override
public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) { public void onSurfaceTextureAvailable(SurfaceTexture texture, int width, int height) {
configureTransform(width, height); configureTransform(width, height);
if (!mTaken) {
mTaken = true;
mMessageHandler.postDelayed(new Runnable() {
@Override
public void run() {
takePicture();
}
}, 400);
}
} }
@Override @Override
public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) { public void onSurfaceTextureSizeChanged(SurfaceTexture texture, int width, int height) {
configureTransform(width, height); configureTransform(width, height);
if (!mTaken) {
mTaken = true;
mMessageHandler.postDelayed(new Runnable() {
@Override
public void run() {
takePicture();
}
}, 400);
}
} }
@Override @Override
@ -558,6 +585,23 @@ public class RawActivity extends AppCompatActivity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Window win = getWindow();
int flags = /*WindowManager.LayoutParams.FLAG_FULLSCREEN
| */WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
int mask = /*WindowManager.LayoutParams.FLAG_FULLSCREEN |
| */WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD;
win.setFlags(flags, mask);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setShowWhenLocked(true);
}
setContentView(R.layout.activity_raw); setContentView(R.layout.activity_raw);
mTextureView = (AutoFitTextureView) findViewById(R.id.texture); mTextureView = (AutoFitTextureView) findViewById(R.id.texture);
@ -590,12 +634,7 @@ public class RawActivity extends AppCompatActivity {
MicroPhotoService.infoLog("RawActivity created PHOTOID=" + Long.toString(mPhotoId)); MicroPhotoService.infoLog("RawActivity created PHOTOID=" + Long.toString(mPhotoId));
mTextureView.setAspectRatio(width, height); mTextureView.setAspectRatio(width, height);
}
@Override
public void onResume() {
super.onResume();
startBackgroundThread(); startBackgroundThread();
openCamera(); openCamera();
@ -605,28 +644,35 @@ public class RawActivity extends AppCompatActivity {
// the SurfaceTextureListener). // the SurfaceTextureListener).
if (mTextureView.isAvailable()) { if (mTextureView.isAvailable()) {
configureTransform(mTextureView.getWidth(), mTextureView.getHeight()); configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
mMessageHandler.postDelayed(new Runnable() {
@Override
public void run() {
takePicture();
}
}, 400);
} else { } else {
mTextureView.setSurfaceTextureListener(mSurfaceTextureListener); mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
} }
if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) { if (mOrientationListener != null && mOrientationListener.canDetectOrientation()) {
mOrientationListener.enable(); mOrientationListener.enable();
} }
}
@Override
public void onResume() {
super.onResume();
MicroPhotoService.infoLog("RawActivity onResume PHOTOID=" + Long.toString(mPhotoId));
mMessageHandler.postDelayed(new Runnable() {
@Override
public void run() {
takePicture();
}
}, 400);
} }
@Override @Override
public void onPause() { public void onPause() {
if (mOrientationListener != null) {
mOrientationListener.disable(); MicroPhotoService.infoLog("RawActivity onPause PHOTOID=" + Long.toString(mPhotoId));
}
closeCamera();
stopBackgroundThread();
super.onPause(); super.onPause();
} }
@ -634,6 +680,12 @@ public class RawActivity extends AppCompatActivity {
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
if (mOrientationListener != null) {
mOrientationListener.disable();
}
closeCamera();
stopBackgroundThread();
MicroPhotoService.infoLog("RawActivity destroyed PHOTOID=" + Long.toString(mPhotoId)); MicroPhotoService.infoLog("RawActivity destroyed PHOTOID=" + Long.toString(mPhotoId));
} }

@ -4,6 +4,7 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".video.RawActivity"> tools:context=".video.RawActivity">
<com.xypower.mpapp.video.AutoFitTextureView <com.xypower.mpapp.video.AutoFitTextureView

Loading…
Cancel
Save