首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >同时扫描QR码和拍摄QR码?

同时扫描QR码和拍摄QR码?
EN

Stack Overflow用户
提问于 2021-08-30 03:18:42
回答 1查看 226关注 0票数 0

我有一个使用ZXing库的QR代码扫描器,现在我只需要从图像中保存QR代码。我试着拍照,但总是在不同的活动中。像扫描一样,活动和拍照是不一样的。所以我要做两次才能获得QR代码。

  1. 扫描QR代码
  2. 拍照。我只想这样做一次,而不是拍照活动。

我试着用这个cameraIntent.putExtra("android.intent.extra.quickCapture", true);,但它似乎是正常的拍照。

我还发现了一个类似于我现在所做的应用。QR &条码扫描器

从这里:

to此文:

我想做的是像扫描QR代码,只裁剪QR代码部分,并向数据库发送请求。或者无论如何都要帮忙,真的很感激。

EN

回答 1

Stack Overflow用户

发布于 2021-08-30 09:00:22

同时扫描QR码和图像QR码

如果您的意思是扫描qr代码并将qr代码显示为图像,那么您可以按照答案进行操作。

通过integrator.initiateScan();扫描qr代码,然后在onActivityResult上通过MultiFormatWriterBarcodeEncoder创建结果位图,并在任何ImageView中显示位图并生成任何TextView。

代码语言:javascript
运行
复制
    MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try {
                BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), BarcodeFormat.QR_CODE, 200, 200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }

基于您附加的图片,我共享一个完整的活动和布局文件,这将涵盖您的需求。这将扫描qr代码,显示结果为图像和文本也。如果结果包含任何url,那么它将使其成为可点击的Linkify。很少有按钮添加到通过ClipboardManager复制结果,通过意图共享结果。

receive.xml

代码语言:javascript
运行
复制
 <?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: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="10dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linear1"
        android:orientation="horizontal">

        <Button
            android:id="@+id/openButton"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="50dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="Open"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="normal" />

        <Button
            android:id="@+id/searchButton"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="50dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="Search"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="normal" />

        <Button
            android:id="@+id/copyButton"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="50dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="Copy"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="normal" />

        <Button
            android:id="@+id/shareButton"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:layout_marginTop="50dp"
            android:layout_weight="1"
            android:background="@android:color/transparent"
            android:text="Share"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="normal" />

    </LinearLayout>

        <TextView
        android:id="@+id/someText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:gravity="center"
        android:textColor="@android:color/black" />

     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:layout_below="@+id/someText">

         <ImageView
             android:id="@+id/image"
             android:layout_width="match_parent"
             android:layout_height="match_parent" />

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="520dp"
        android:layout_centerHorizontal="true"
        android:text="SCAN AGAIN"
        android:theme="@style/ConnectButton"
        android:id="@+id/scan_btn"/>
</RelativeLayout>

ReceiveS活性-

代码语言:javascript
运行
复制
public class ReceiveS extends AppCompatActivity {
public static Button scan_btn = null;
ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.receive);

    image = (ImageView) findViewById(R.id.image);

    // show custom alert dialog
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(true);
    dialog.setContentView(R.layout.receive);


    TextView text = (TextView) findViewById(R.id.someText);
    final Activity activity = this;

    scan_btn = (Button) findViewById(R.id.scan_btn);
    scan_btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            integrator.setPrompt("Scan");
            integrator.setCameraId(0);
            integrator.setBeepEnabled(false);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    TextView text = (TextView) findViewById(R.id.someText);


    final IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
    if (result != null) {
        if (result.getContents() == null) {
            Toast.makeText(this, "You cancelled scanning", Toast.LENGTH_LONG).show();
        } else {

            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            try {
                BitMatrix bitMatrix = multiFormatWriter.encode(result.getContents(), BarcodeFormat.QR_CODE, 200, 200);
                BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
                Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);
                image.setImageBitmap(bitmap);
            } catch (WriterException e) {
                e.printStackTrace();
            }

            SpannableString url = SpannableString.valueOf(result.getContents());
            Linkify.addLinks(url, Linkify.WEB_URLS);
            text.setText(url);
            text.setMovementMethod(LinkMovementMethod.getInstance());


            Button openlink = (Button) findViewById(R.id.openButton);
            Button webSearch = (Button) findViewById(R.id.searchButton);
            Button copy = (Button) findViewById(R.id.copyButton);
            Button share = (Button) findViewById(R.id.shareButton);

            openlink.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = result.getContents();
                    if (url.startsWith("https://") || url.startsWith("http://")) {
                        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                        startActivity(intent);
                    } else {
                        Toast.makeText(ReceiveS.this, "Invalid Url", Toast.LENGTH_SHORT).show();
                    }

                }
            });

            webSearch.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url;
                    url = "http://www.google.com/#q=" + result.getContents();
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    startActivity(intent);
                }
            });
            copy.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                    ClipData clip = ClipData.newPlainText("", result.getContents());
                    clipboard.setPrimaryClip(clip);

                    Toast.makeText(getApplicationContext(), "Copied to clipboard", Toast.LENGTH_SHORT).show();

                }
            });
            share.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                    sharingIntent.setType("text/plain");
                    String shareBody = result.getContents();
                    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
                    startActivity(Intent.createChooser(sharingIntent, "Share "));

                }
            });


        }
    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68978601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档