优化操作UI

main
Matthew 8 months ago
parent 73a8dd7216
commit d82d8a7540

@ -20,8 +20,8 @@ repositories {
}
dependencies {
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
}
// The sample build uses multiple directories to
@ -37,12 +37,10 @@ android {
defaultConfig {
minSdkVersion 24
//noinspection ExpiredTargetSdkVersion
targetSdkVersion 27
}
lint {
baseline = file("lint-baseline.xml")
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<issues format="6" by="lint 7.2.1" type="baseline" client="gradle" dependencies="false" name="AGP (7.2.1)" variant="fatal" version="7.2.1">
<issue
id="ExpiredTargetSdkVersion"
message="Google Play requires that apps target API level 30 or higher.&#xA;"
errorLine1=" targetSdkVersion 27"
errorLine2=" ~~~~~~~~~~~~~~~~~~~">
<location
file="build.gradle"
line="40"
column="9"/>
</issue>
</issues>

@ -1,47 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xypower.mppreview"
android:versionCode="1"
android:versionName="1.0">
<!-- Min/target SDK versions (<uses-sdk>) managed by build.gradle -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<!-- A camera with RAW capability is required to use this application -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <!-- A camera with RAW capability is required to use this application -->
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.raw" />
<application android:allowBackup="true"
android:label="@string/app_name"
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MaterialTheme">
<activity android:name="com.xypower.mppreview.CameraActivity"
android:screenOrientation="landscape"
android:label="@string/app_name">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".CameraActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
</activity>
</application>
</manifest>
</manifest>

@ -24,12 +24,17 @@ import android.app.DialogFragment;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.Drawable;
import android.hardware.SensorManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
@ -55,7 +60,7 @@ import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.ActivityCompat;
import androidx.core.app.ActivityCompat;
import android.text.TextUtils;
import android.util.Log;
import android.util.Size;
@ -66,6 +71,7 @@ import android.view.Surface;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.Toast;
@ -556,8 +562,12 @@ public class Camera2RawFragment extends Fragment
}
};
public static Camera2RawFragment newInstance() {
return new Camera2RawFragment();
public static Camera2RawFragment newInstance(final boolean hdr) {
Camera2RawFragment fragment = new Camera2RawFragment();
Bundle bundle = new Bundle();
return fragment;
}
@Override
@ -568,18 +578,22 @@ public class Camera2RawFragment extends Fragment
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
Resources resources = getResources();
ImageView imageView = (ImageView)view.findViewById(R.id.picture);
imageView.setImageDrawable(resources.getDrawable(R.drawable.ic_take_photo));
imageView = (ImageView)view.findViewById(R.id.backMain);
imageView.setImageDrawable(resources.getDrawable(R.drawable.ic_back));
view.findViewById(R.id.picture).setOnClickListener(this);
// view.findViewById(R.id.info).setOnClickListener(this);
mTextureView = (AutoFitTextureView) view.findViewById(R.id.texture);
view.findViewById(R.id.hdr).setOnClickListener(new View.OnClickListener() {
view.findViewById(R.id.backMain).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
closeCamera();
// stopBackgroundThread();
// startBackgroundThread();
openCamera();
getActivity().finish();
}
});
@ -673,11 +687,9 @@ public class Camera2RawFragment extends Fragment
return false;
}
View rootView = getView();
Switch hdrSwitch = (Switch)(rootView.findViewById(R.id.hdr));
Intent intent = activity.getIntent();
String expectedCameraId = Integer.toString(intent.getIntExtra("cameraId", 0));
boolean hdrChecked = hdrSwitch.isChecked();
final String expectedCameraId = hdrChecked ? "1" : "0";
try {
// Find a CameraDevice that supports RAW captures, and configure state.
for (String cameraId : manager.getCameraIdList()) {
@ -1017,7 +1029,7 @@ public class Camera2RawFragment extends Fragment
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
// Find the rotation of the device relative to the camera sensor's orientation.
int totalRotation = sensorToDeviceRotation(mCharacteristics, deviceRotation);
int totalRotation = sensorToDeviceRotation(mCharacteristics, deviceRotation, getRotationAdjustment());
// Swap the view dimensions for calculation as needed if they are rotated relative to
// the sensor.
@ -1060,8 +1072,8 @@ public class Camera2RawFragment extends Fragment
// cameras).
int rotation = (mCharacteristics.get(CameraCharacteristics.LENS_FACING) ==
CameraCharacteristics.LENS_FACING_FRONT) ?
(360 + ORIENTATIONS.get(deviceRotation)) % 360 :
(360 - ORIENTATIONS.get(deviceRotation)) % 360;
(360 + ORIENTATIONS.get(deviceRotation + getRotationAdjustment() / 90)) % 360 :
(360 - ORIENTATIONS.get(deviceRotation + getRotationAdjustment() / 90)) % 360;
Matrix matrix = new Matrix();
RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
@ -1183,7 +1195,7 @@ public class Camera2RawFragment extends Fragment
// Set orientation.
int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
captureBuilder.set(CaptureRequest.JPEG_ORIENTATION,
sensorToDeviceRotation(mCharacteristics, rotation));
sensorToDeviceRotation(mCharacteristics, rotation, getRotationAdjustment()));
// Set request tag to easily track results in callbacks.
captureBuilder.setTag(mRequestCounter.getAndIncrement());
@ -1329,20 +1341,32 @@ public class Camera2RawFragment extends Fragment
int format = mImage.getFormat();
switch (format) {
case ImageFormat.JPEG: {
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
FileOutputStream output = null;
try {
output = new FileOutputStream(mFile);
output.write(bytes);
success = true;
if (mCharacteristics.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
Bitmap bm2 = flipBitmap(bm);
bm.recycle();
success = bm2.compress(Bitmap.CompressFormat.JPEG, 95, output);
bm2.recycle();
} else {
output.write(bytes);
success = true;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
closeOutput(output);
}
break;
}
case ImageFormat.RAW_SENSOR: {
@ -1387,6 +1411,20 @@ public class Camera2RawFragment extends Fragment
}
}
private Bitmap flipBitmap(Bitmap bitmap) {
if (null == bitmap) {
return null;
}
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Matrix m = new Matrix();
m.postScale(-1, 1); //镜像水平翻转
//m.postScale(1, -1); //镜像垂直翻转
//m.postRotate(-90); //旋转-90度
Bitmap new2 = Bitmap.createBitmap(bitmap, 0, 0, width, height, m, true);
return new2;
}
/**
* Builder class for constructing {@link ImageSaver}s.
* <p/>
@ -1679,6 +1717,10 @@ public class Camera2RawFragment extends Fragment
return Math.abs(aAspect - bAspect) <= ASPECT_RATIO_TOLERANCE;
}
private int getRotationAdjustment() {
return TextUtils.equals(mCameraId, "1") ? 90 : 0;
}
/**
* Rotation need to transform from the camera sensor orientation to the device's current
* orientation.
@ -1689,11 +1731,11 @@ public class Camera2RawFragment extends Fragment
* orientation.
* @return the total rotation from the sensor orientation to the current device orientation.
*/
private static int sensorToDeviceRotation(CameraCharacteristics c, int deviceOrientation) {
private static int sensorToDeviceRotation(CameraCharacteristics c, int deviceOrientation, int adjustment) {
int sensorOrientation = c.get(CameraCharacteristics.SENSOR_ORIENTATION);
// Get device orientation in degrees
deviceOrientation = ORIENTATIONS.get(deviceOrientation);
deviceOrientation = ORIENTATIONS.get((deviceOrientation + (adjustment / 90)) % ORIENTATIONS.size());
// Reverse device orientation for front-facing cameras
if (c.get(CameraCharacteristics.LENS_FACING) == CameraCharacteristics.LENS_FACING_FRONT) {
@ -1702,6 +1744,7 @@ public class Camera2RawFragment extends Fragment
// Calculate desired JPEG orientation relative to camera orientation to make
// the image upright relative to the device orientation
return (sensorOrientation - deviceOrientation + 360) % 360;
}

@ -18,21 +18,38 @@ package com.xypower.mppreview;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
/**
* Activity displaying a fragment that implements RAW photo captures.
*/
public class CameraActivity extends Activity {
Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mHandler = new Handler();
setContentView(R.layout.activity_camera);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2RawFragment.newInstance())
.replace(R.id.container, Camera2RawFragment.newInstance(false))
.commit();
}
}
public void reopenFragment(final boolean hdr) {
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2RawFragment.newInstance(hdr))
.commit();
}
}, 0);
}
}

@ -0,0 +1,54 @@
package com.xypower.mppreview;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.channel1)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto(1);
}
});
((Button)findViewById(R.id.channel2)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto(2);
}
});
((Button)findViewById(R.id.channel3)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
takePhoto(3);
}
});
}
protected void takePhoto(int channel) {
Switch hdrSwitch = (Switch) findViewById(R.id.hdr);
takePhoto(channel, hdrSwitch.isChecked());
}
protected void takePhoto(int channel, boolean hdr) {
int cameraId = channel - 1;
if (channel == 1 && hdr) {
cameraId = 1;
}
Intent intent = new Intent(MainActivity.this, CameraActivity.class);
intent.putExtra("cameraId", cameraId);
startActivity(intent);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

@ -0,0 +1,5 @@
<vector android:height="48dp" android:tint="#0000FF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="48dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

@ -0,0 +1,6 @@
<vector android:height="24dp" android:tint="#0000FF"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path android:fillColor="@android:color/white" android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 B

@ -13,48 +13,55 @@
See the License for the specific language governing permissions and
limitations under the License.
-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.xypower.mppreview.AutoFitTextureView
android:id="@+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_below="@id/texture"
android:layout_toEndOf="@id/texture"
android:layout_height="match_parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#504285f4"
android:orientation="vertical">
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<Button
<ImageView
android:id="@+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:text="@string/picture" />
<Switch
android:id="@+id/hdr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="HDR"
android:textOff="HDR"/>
android:scaleType="fitXY"
app:layout_constraintBottom_toTopOf="@+id/backMain"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.5"
app:srcCompat="@drawable/ic_take_photo"
/>
<ImageView
android:id="@+id/backMain"
android:layout_width="48dp"
android:layout_height="48dp"
android:scaleType="fitXY"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/picture"
app:srcCompat="@drawable/ic_back"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/channel1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="通道1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/channel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="通道2"
app:layout_constraintStart_toEndOf="@+id/channel1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/channel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:text="通道3"
app:layout_constraintStart_toEndOf="@+id/channel2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:text="HDR"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/channel1" />
<Switch
android:id="@+id/hdr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toEndOf="@id/textView"
app:layout_constraintTop_toTopOf="@id/textView"
app:layout_constraintBottom_toBottomOf="@id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>

@ -0,0 +1,2 @@
android.enableJetifier=true
android.useAndroidX=true
Loading…
Cancel
Save