- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我一直在关注以下tutorial ,它使用移动视觉 api 创建条形码/二维码扫描仪。虽然我已经输入了它所说的确切代码(至少我认为),但来自 cameraSource
的摄像头馈送。
我在这个过程中添加了一些额外的代码,看看它是否会有所作为,但它仍然没有。问题是当我启动应用程序时,SurfaceView 应该显示 CAMERA_FACING_BACK
提要,但它是纯黑色的。如果您能告诉我为什么提要没有显示,如果有的话,代码需要更改,我们将不胜感激。
主要 Activity .java:
package com.example.neekondev.barcodeshortened;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import java.io.IOException;
import static com.google.android.gms.vision.CameraSource.CAMERA_FACING_BACK;
import static com.google.android.gms.vision.CameraSource.CAMERA_FACING_FRONT;
public class MainActivity extends AppCompatActivity {
SurfaceView cameraView;
TextView barcodeInfo;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cameraView = (SurfaceView) findViewById(R.id.camera_view);
barcodeInfo = (TextView) findViewById(R.id.code_info);
barcodeDetector =
new BarcodeDetector.Builder(this)
.setBarcodeFormats(Barcode.CODE_39 | Barcode.CODE_93 | Barcode.CODE_128)
.build();
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setRequestedPreviewSize(640, 480)
.setRequestedFps(20.0f)
.setFacing(CAMERA_FACING_BACK)
.build();
cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.CAMERA) !=
PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
cameraSource.start(cameraView.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>() {
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
if (barcodes.size() != 0) {
barcodeInfo.post(new Runnable() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText( // Update the TextView
barcodes.valueAt(0).displayValue
);
}
});
}
}
});
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.neekondev.barcodeshortened.MainActivity">
<SurfaceView
android:layout_width="640px"
android:layout_height="480px"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:id="@+id/camera_view"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/code_info"
android:layout_toRightOf="@+id/camera_view"
android:textSize="20sp"
android:layout_marginLeft="16dp"
android:text="Nothing yet..."
android:layout_alignParentTop="true"
/>
</RelativeLayout>
list .xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.neekondev.barcodeshortened">
<meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.Camera"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
最佳答案
没关系,这是我自己想出来的,虽然我认为其他人可能会在教程结束后问同样的问题,所以我会告诉你哪里出了问题。你看,虽然我在Manifest中声明了权限,但是android.permissions.CAMERA
是一个“高风险权限”,也就是说需要用户手动同意才可能被执行。因为我从未制作过直接向用户请求的权限对话框,所以无法执行所有使用相机权限的代码,从而给了我们空白屏幕与相机的提要。一个简单的解决方法是进入设置并在那里允许权限,但也可以使用以下代码,通过对话框方便地请求相机权限;
import android.Manifest;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.media.Image;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import static junit.framework.Assert.assertNotNull;
public class MainActivity
extends AppCompatActivity {
FirebaseDatabase database;
DatabaseReference myRef;
SurfaceView cameraView;
TextView barcodeInfo;
BarcodeDetector barcodeDetector;
CameraSource cameraSource;
private static final int CAMERA_PERMISSION_CAMERA = 0x000000;
public static boolean position = false;
//Camera cam;
//Camera.Parameters p;
@Override
protected void onCreate(Bundle savedInstanceState) {
setRequestedOrientation(ActivityInfo
.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(MainActivity
.this,
Manifest
.permission
.CAMERA)
!= PackageManager
.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity
.this,
Manifest
.permission
.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity
.this,
new String[]{Manifest.permission
.CAMERA},
CAMERA_PERMISSION_CAMERA);
// CAMERA_PERMISSION_CAMERA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
cameraView = (SurfaceView) findViewById(R
.id
.camera_view);
barcodeInfo = (TextView) findViewById(R
.id
.code_info);
MainActivity
.this
.getPackageManager()
.hasSystemFeature
(PackageManager
.FEATURE_CAMERA_FLASH);
barcodeDetector =
new BarcodeDetector.Builder(getApplicationContext())
.setBarcodeFormats(Barcode.ALL_FORMATS)
.build();
cameraSource = new CameraSource
.Builder(getApplicationContext(), barcodeDetector)
.setFacing(CameraSource.CAMERA_FACING_BACK)
.setRequestedFps(35.0f)
.setRequestedPreviewSize(960, 960)
.setAutoFocusEnabled(true)
.build();
//setupButtons();
cameraView.getHolder().addCallback(new SurfaceHolder
.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// TODO: CONSIDER CALLING
//ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
cameraSource.start(cameraView
.getHolder());
} catch (IOException ie) {
Log.e("CAMERA SOURCE", ie
.getMessage());
}
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format,
int width,
int height){}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
cameraSource.stop();
}
});
barcodeDetector.setProcessor(new Detector.Processor<Barcode>()
{
@Override
public void release() {
}
@Override
public void receiveDetections(Detector.Detections<Barcode> detections)
{
final SparseArray<Barcode> barcodes = detections.getDetectedItems();
database = FirebaseDatabase.getInstance();
myRef = database
.getReference(getTime());
if (barcodes
.size() != 0) {
barcodeInfo
.post(new Runnable() { // Use the post method of the TextView
public void run() {
barcodeInfo.setText(barcodes
.valueAt(0)
.displayValue
);
myRef.setValue(barcodes
.valueAt(0)
.displayValue
);
}
});
}
}
});
}
/*
public void FLASH_ON()
{
cam = Camera.open();
p = cam.getParameters();
p.setFlashMode(Camera
.Parameters
.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
public void FLASH_OFF()
{cam.stopPreview();
cam.release();}
public void setupButtons()
{
ImageButton flash = (ImageButton)findViewById(R.id.flash);
flash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
if (position == false)
{
FLASH_ON();
position = true;
} if (position == true)
{
FLASH_OFF();
position = false;
}
}
});
}*/
public String getTime()
{
String downToSeconds = DateFormat
.getDateTimeInstance()
.format(
new Date());
return downToSeconds;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[],
int[] grantResults) {
switch (requestCode) {
case CAMERA_PERMISSION_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0]
== PackageManager
.PERMISSION_GRANTED) {
Intent startMain = new Intent(MainActivity
.this, MainActivity
.class);
startActivity(startMain);
} else {
if (ContextCompat.checkSelfPermission(MainActivity
.this,
Manifest
.permission
.CAMERA)
!= PackageManager
.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity
.this,
Manifest
.permission
.CAMERA)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(MainActivity
.this,
new String[]{Manifest.permission
.CAMERA},
CAMERA_PERMISSION_CAMERA);
// CAMERA_PERMISSION_CAMERA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
return;
}
}
}
}
请原谅我的一些服务器代码。我也将结果上传到 firebase 数据库。其余的是相同的。布局有一些缩放比例的小调整,但其余部分是相同的。祝你好运!
关于java - 移动视觉条码扫描器 : Why does SurfaceView not update CameraSource?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41295955/
我正在编写一个具有以下签名的 Java 方法。 void Logger(Method method, Object[] args); 如果一个方法(例如 ABC() )调用此方法 Logger,它应该
我是 Java 新手。 我的问题是我的 Java 程序找不到我试图用作的图像文件一个 JButton。 (目前这段代码什么也没做,因为我只是得到了想要的外观第一的)。这是我的主课 代码: packag
好的,今天我在接受采访,我已经编写 Java 代码多年了。采访中说“Java 垃圾收集是一个棘手的问题,我有几个 friend 一直在努力弄清楚。你在这方面做得怎么样?”。她是想骗我吗?还是我的一生都
我的 friend 给了我一个谜语让我解开。它是这样的: There are 100 people. Each one of them, in his turn, does the following
如果我将使用 Java 5 代码的应用程序编译成字节码,生成的 .class 文件是否能够在 Java 1.4 下运行? 如果后者可以工作并且我正在尝试在我的 Java 1.4 应用程序中使用 Jav
有关于why Java doesn't support unsigned types的问题以及一些关于处理无符号类型的问题。我做了一些搜索,似乎 Scala 也不支持无符号数据类型。限制是Java和S
我只是想知道在一个 java 版本中生成的字节码是否可以在其他 java 版本上运行 最佳答案 通常,字节码无需修改即可在 较新 版本的 Java 上运行。它不会在旧版本上运行,除非您使用特殊参数 (
我有一个关于在命令提示符下执行 java 程序的基本问题。 在某些机器上我们需要指定 -cp 。 (类路径)同时执行java程序 (test为java文件名与.class文件存在于同一目录下) jav
我已经阅读 StackOverflow 有一段时间了,现在我才鼓起勇气提出问题。我今年 20 岁,目前在我的家乡(罗马尼亚克卢日-纳波卡)就读 IT 大学。足以介绍:D。 基本上,我有一家提供簿记应用
我有 public JSONObject parseXML(String xml) { JSONObject jsonObject = XML.toJSONObject(xml); r
我已经在 Java 中实现了带有动态类型的简单解释语言。不幸的是我遇到了以下问题。测试时如下代码: def main() { def ks = Map[[1, 2]].keySet()
一直提示输入 1 到 10 的数字 - 结果应将 st、rd、th 和 nd 添加到数字中。编写一个程序,提示用户输入 1 到 10 之间的任意整数,然后以序数形式显示该整数并附加后缀。 public
我有这个 DownloadFile.java 并按预期下载该文件: import java.io.*; import java.net.URL; public class DownloadFile {
我想在 GUI 上添加延迟。我放置了 2 个 for 循环,然后重新绘制了一个标签,但这 2 个 for 循环一个接一个地执行,并且标签被重新绘制到最后一个。 我能做什么? for(int i=0;
我正在对对象 Student 的列表项进行一些测试,但是我更喜欢在 java 类对象中创建硬编码列表,然后从那里提取数据,而不是连接到数据库并在结果集中选择记录。然而,自从我这样做以来已经很长时间了,
我知道对象创建分为三个部分: 声明 实例化 初始化 classA{} classB extends classA{} classA obj = new classB(1,1); 实例化 它必须使用
我有兴趣使用 GPRS 构建车辆跟踪系统。但是,我有一些问题要问以前做过此操作的人: GPRS 是最好的技术吗?人们意识到任何问题吗? 我计划使用 Java/Java EE - 有更好的技术吗? 如果
我可以通过递归方法反转数组,例如:数组={1,2,3,4,5} 数组结果={5,4,3,2,1}但我的结果是相同的数组,我不知道为什么,请帮助我。 public class Recursion { p
有这样的标准方式吗? 包括 Java源代码-测试代码- Ant 或 Maven联合单元持续集成(可能是巡航控制)ClearCase 版本控制工具部署到应用服务器 最后我希望有一个自动构建和集成环境。
我什至不知道这是否可能,我非常怀疑它是否可能,但如果可以,您能告诉我怎么做吗?我只是想知道如何从打印机打印一些文本。 有什么想法吗? 最佳答案 这里有更简单的事情。 import javax.swin
我是一名优秀的程序员,十分优秀!