forked from PaddlePaddle/PaddleOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refine UI of android demo, refactoring code to make it easy to add ne…
…w demo such as object detection, support reading model from sdcard (PaddlePaddle#7) test=develop
- Loading branch information
1 parent
b7d9cb5
commit 8a2db5d
Showing
21 changed files
with
982 additions
and
309 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
...ndroid-demo/app/src/main/java/com/baidu/paddle/lite/demo/AppCompatPreferenceActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright (C) 2014 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. | ||
*/ | ||
|
||
package com.baidu.paddle.lite.demo; | ||
|
||
import android.content.res.Configuration; | ||
import android.os.Bundle; | ||
import android.preference.PreferenceActivity; | ||
import android.support.annotation.LayoutRes; | ||
import android.support.annotation.Nullable; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatDelegate; | ||
import android.support.v7.widget.Toolbar; | ||
import android.view.MenuInflater; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
|
||
/** | ||
* A {@link android.preference.PreferenceActivity} which implements and proxies the necessary calls | ||
* to be used with AppCompat. | ||
* <p> | ||
* This technique can be used with an {@link android.app.Activity} class, not just | ||
* {@link android.preference.PreferenceActivity}. | ||
*/ | ||
public abstract class AppCompatPreferenceActivity extends PreferenceActivity { | ||
private AppCompatDelegate mDelegate; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
getDelegate().installViewFactory(); | ||
getDelegate().onCreate(savedInstanceState); | ||
super.onCreate(savedInstanceState); | ||
} | ||
|
||
@Override | ||
protected void onPostCreate(Bundle savedInstanceState) { | ||
super.onPostCreate(savedInstanceState); | ||
getDelegate().onPostCreate(savedInstanceState); | ||
} | ||
|
||
public ActionBar getSupportActionBar() { | ||
return getDelegate().getSupportActionBar(); | ||
} | ||
|
||
public void setSupportActionBar(@Nullable Toolbar toolbar) { | ||
getDelegate().setSupportActionBar(toolbar); | ||
} | ||
|
||
@Override | ||
public MenuInflater getMenuInflater() { | ||
return getDelegate().getMenuInflater(); | ||
} | ||
|
||
@Override | ||
public void setContentView(@LayoutRes int layoutResID) { | ||
getDelegate().setContentView(layoutResID); | ||
} | ||
|
||
@Override | ||
public void setContentView(View view) { | ||
getDelegate().setContentView(view); | ||
} | ||
|
||
@Override | ||
public void setContentView(View view, ViewGroup.LayoutParams params) { | ||
getDelegate().setContentView(view, params); | ||
} | ||
|
||
@Override | ||
public void addContentView(View view, ViewGroup.LayoutParams params) { | ||
getDelegate().addContentView(view, params); | ||
} | ||
|
||
@Override | ||
protected void onPostResume() { | ||
super.onPostResume(); | ||
getDelegate().onPostResume(); | ||
} | ||
|
||
@Override | ||
protected void onTitleChanged(CharSequence title, int color) { | ||
super.onTitleChanged(title, color); | ||
getDelegate().setTitle(title); | ||
} | ||
|
||
@Override | ||
public void onConfigurationChanged(Configuration newConfig) { | ||
super.onConfigurationChanged(newConfig); | ||
getDelegate().onConfigurationChanged(newConfig); | ||
} | ||
|
||
@Override | ||
protected void onStop() { | ||
super.onStop(); | ||
getDelegate().onStop(); | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
getDelegate().onDestroy(); | ||
} | ||
|
||
public void invalidateOptionsMenu() { | ||
getDelegate().invalidateOptionsMenu(); | ||
} | ||
|
||
private AppCompatDelegate getDelegate() { | ||
if (mDelegate == null) { | ||
mDelegate = AppCompatDelegate.create(this, null); | ||
} | ||
return mDelegate; | ||
} | ||
} |
172 changes: 172 additions & 0 deletions
172
PaddleLite-android-demo/app/src/main/java/com/baidu/paddle/lite/demo/CommonActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
package com.baidu.paddle.lite.demo; | ||
|
||
import android.Manifest; | ||
import android.content.ContentResolver; | ||
import android.content.Intent; | ||
import android.content.pm.PackageManager; | ||
import android.database.Cursor; | ||
import android.graphics.Bitmap; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.provider.MediaStore; | ||
import android.support.annotation.NonNull; | ||
import android.support.v4.app.ActivityCompat; | ||
import android.support.v4.content.ContextCompat; | ||
import android.support.v7.app.ActionBar; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.util.Log; | ||
import android.view.Menu; | ||
import android.view.MenuInflater; | ||
import android.view.MenuItem; | ||
import android.widget.Toast; | ||
|
||
import java.io.IOException; | ||
|
||
public class CommonActivity extends AppCompatActivity { | ||
private static final String TAG = CommonActivity.class.getSimpleName(); | ||
public static final int OPEN_GALLERY_REQUEST_CODE = 0; | ||
public static final int TAKE_PHOTO_REQUEST_CODE = 1; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
ActionBar supportActionBar = getSupportActionBar(); | ||
if (supportActionBar != null) { | ||
supportActionBar.setDisplayHomeAsUpEnabled(true); | ||
} | ||
|
||
} | ||
|
||
public void onImageChanged(Bitmap image) { | ||
} | ||
|
||
public void onDeviceChanged(MenuItem item) { | ||
} | ||
|
||
public void onSettingsClicked() { | ||
} | ||
|
||
@Override | ||
public boolean onCreateOptionsMenu(Menu menu) { | ||
MenuInflater inflater = getMenuInflater(); | ||
inflater.inflate(R.menu.menu_action_options, menu); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(MenuItem item) { | ||
switch (item.getItemId()) { | ||
case android.R.id.home: | ||
finish(); | ||
break; | ||
case R.id.open_gallery: | ||
if (requestAllPermissions()) { | ||
openGallery(); | ||
} | ||
break; | ||
case R.id.take_photo: | ||
if (requestAllPermissions()) { | ||
takePhoto(); | ||
} | ||
break; | ||
case R.id.run_on_cpu: | ||
if (!item.isChecked()) { | ||
onDeviceChanged(item); | ||
item.setChecked(true); | ||
} | ||
break; | ||
case R.id.run_on_npu: | ||
if (!item.isChecked()) { | ||
onDeviceChanged(item); | ||
item.setChecked(true); | ||
} | ||
break; | ||
case R.id.settings: | ||
if (requestAllPermissions()) { | ||
// make sure we have SDCard r&w permissions to load model from SDCard | ||
onSettingsClicked(); | ||
} | ||
break; | ||
} | ||
return super.onOptionsItemSelected(item); | ||
} | ||
|
||
@Override | ||
public boolean onPrepareOptionsMenu(Menu menu) { | ||
menu.findItem(R.id.run_on_npu).setEnabled(Utils.isSupportedNPU()); | ||
if (!Utils.isSupportedNPU()) { | ||
Toast.makeText(CommonActivity.this, "Kirin 810's NPU is the only supported NPU in Paddle-Lite, but we " + | ||
"will add support for more HUAWEI NPUs in the future!", Toast.LENGTH_LONG).show(); | ||
} | ||
return super.onPrepareOptionsMenu(menu); | ||
} | ||
|
||
@Override | ||
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, | ||
@NonNull int[] grantResults) { | ||
super.onRequestPermissionsResult(requestCode, permissions, grantResults); | ||
if (grantResults[0] != PackageManager.PERMISSION_GRANTED || grantResults[1] != PackageManager.PERMISSION_GRANTED) { | ||
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show(); | ||
} | ||
} | ||
|
||
private boolean requestAllPermissions() { | ||
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) | ||
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, | ||
Manifest.permission.CAMERA) | ||
!= PackageManager.PERMISSION_GRANTED) { | ||
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, | ||
Manifest.permission.CAMERA}, | ||
0); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
private void openGallery() { | ||
Intent intent = new Intent(Intent.ACTION_PICK, null); | ||
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); | ||
startActivityForResult(intent, OPEN_GALLERY_REQUEST_CODE); | ||
} | ||
|
||
private void takePhoto() { | ||
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | ||
if (takePhotoIntent.resolveActivity(getPackageManager()) != null) { | ||
startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST_CODE); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
super.onActivityResult(requestCode, resultCode, data); | ||
if (resultCode == RESULT_OK && data != null) { | ||
switch (requestCode) { | ||
case OPEN_GALLERY_REQUEST_CODE: | ||
try { | ||
ContentResolver resolver = getContentResolver(); | ||
Uri originalUri = data.getData(); | ||
Bitmap imageData = MediaStore.Images.Media.getBitmap(resolver, originalUri); | ||
String[] proj = {MediaStore.Images.Media.DATA}; | ||
Cursor cursor = managedQuery(originalUri, proj, null, null, null); | ||
cursor.moveToFirst(); | ||
onImageChanged(imageData); | ||
} catch (IOException e) { | ||
Log.e(TAG, e.toString()); | ||
} | ||
break; | ||
case TAKE_PHOTO_REQUEST_CODE: | ||
Bundle extras = data.getExtras(); | ||
Bitmap imageData = (Bitmap) extras.get("data"); | ||
onImageChanged(imageData); | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
protected void onResume() { | ||
super.onResume(); | ||
} | ||
} |
Oops, something went wrong.